2 Replies Latest reply on May 12, 2017 12:49 PM by cmoke73

    replace groups and their content

    cmoke73

      Hi!

      This shouldn´t be a question but a post about a short script I worked on for a week (the destiny of each newbee).

      It works in larger documents. Of course there is a need for a library and its elements, special kind of items the script searches for and an open document with some pages.

       

      I´m sure there are many other ways to make the script more short and with higher performance. Feel free to leave your opinion...

       

       

      var myDoc = app.activeDocument;
      var myObj = myDoc.selection[0];
      var myPages = myDoc.pages.everyItem();
      var numberOfPages = myDoc.pages.length;
      
      var lastPgNumber = myDoc.pages.lastItem().name;
      
      var newLabel; 
      var myNewLabel;
      var myGroups = myDoc.groups;
      var allMyGroups = myGroups.length;
      var allMyObjects;
      var myPageNumber; var oldObjPgNr;
      var myObjects = [],
      badObj = [],
      myText = [],
      myRectPositionY1 = [],
      myRectPositionX1 = []
      newPagesArray = [];
      
      app.doScript(deleteObj, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Preislabel ändern");
      
      function deleteObj() {
          for (var i=0; i < allMyGroups; i++) {
              var thisGroup = myGroups[i];
              //selecting groups (I only need the groups ON pages, with TWO items inside, one of the items should be a RECTANGLE) ...
              if (thisGroup.parentPage != null && thisGroup.pageItems.length == 2 && thisGroup.allPageItems[1].constructor.name == "Rectangle") {
                  // ... in addition: the rectangle should be WHITE
                  if(thisGroup.rectangles[0].fillColor.name == "Paper") {
                      // add all "useful" groups in an ARRAY
                      myObjects.push(thisGroup); 
                  }
              }
                 //else if (badObj.push(thisGroup));
          }
      allMyObjects = myObjects.length;
      for(var n=0; n < allMyObjects; n++){
          // calculate the "real" PAGE-NUMBER
          oldObjPgNr = myObjects[n].parentPage.name;
          myPageNumber = numberOfPages - (lastPgNumber - oldObjPgNr)-1; 
          
          // get coordinates for the current object
          myRectPositionY1.push(myObjects[n].geometricBounds[1]+ 6);
          myRectPositionX1.push(myObjects[n].geometricBounds[0] );
          
          // grab new object from  ibrary
          newLabel = app.libraries.itemByName("SF_Kataloge_Zutaten.indl").assets.itemByName("SF_Preis_rot_NEU");
          myNewLabel = newLabel.placeAsset(myDoc);
          
          // move new object to the same page and location like the current object
          myNewLabel[0].move(myDoc.pages[myPageNumber]);    
          myNewLabel[0].move([myRectPositionY1[n], myRectPositionX1[n]])
          
          // replace contents of the new object with the contents of the current object
          myNewLabel[0].contents = myObjects[n].textFrames[0].contents;
           }
       for(var b=0; b < allMyObjects; b++){
           // delete all groups
          myObjects[b].remove();
      }   
      
      }   
      

       

      Regards

      A.

       

      ps. It shouldn´t be forgetted: of course I had a lot of help from the community here!!! Thank you by the way!

        • 1. Re: replace groups and their content
          cmoke73 Level 1

          Okay, little problem:

          In one doc it causes no error. But in the others suddenly the object gets "not valid".

          Line 29 & line 55 will be marked.

          I tried to find out which object causes the mess with:

           

          alert(thisGroup.parentPage.name + "\r" + thisGroup.textFrames[0].contents);
          

           

          ... added after line 29. I can´t find anything wrong...

           

          Feedback after fixing that problem.

           

          regrets...

          I meen regards

          A.

          • 2. Re: replace groups and their content
            cmoke73 Level 1

            The problem was: there are not only rectangles in the documents but also POLYGONS :

             

            ...

            for (var i=0; i < allMyGroups; i++) {
            
            var thisGroup = myGroups[i];
            
            resolvedItem = app.documents[0].groups[i].pageItems[0].getElements()[0];  
            
            if(thisGroup.parentPage != null && resolvedItem.parent.allPageItems.length == 2)  {
            
            
            
            if(resolvedItem.constructor.name == "Rectangle" || resolvedItem.constructor.name == "Polygon") {
            
            
            
            if(resolvedItem.fillColor.name == "Paper"){
            
            myObjects.push(thisGroup);   
            
            
            }
            
            }
            
            };  
            
                
            
            }
            
            

            ...

             

            (thank U Uwe: count number of objects in a group )