3 Replies Latest reply on Jul 30, 2015 12:10 PM by Synkx

    Move graphics and texts to separate layers on multiple opened documents

    Synkx

      Hi everyone,

       

      My situation is as follows :

      I've got multiple documents to make changes to, and I can't have my script to work out on all those open documents.

      I'm getting an error :

      2015-07-30_12h12_09.png

       

      My script is working on one file, only one, and then stops with this error.

       

      What I'm trying to do :


      I would like to move all my texts to a certain layer, that is created by the script, and move all my pictures to another layer, also created by the script. (the graphics must be only graphics, not rectangles with a fillColor, but rectangles with graphics inside of them.)

      I'm trying to make this possible on every opened documents.

      I also would like to get rid of all other layers, except the two I'm creating with the script.

       

      Why am I doing this ?

      Well, I've got documents with images and texts, but also some rectangles filled with colors.

      I'm trying to get rid of these rectangles, but I didn't find any way to get rid of rectangles without getting rid of rectangles that have images inside of them...

       

      Here's the code I'm working on :

       

      function moveToLayer(){    
       var myDoc = app.documents;
      
              for (var i = myDoc.length-1; i >= 0; i--) {
                  
      if(!myDoc[i].layers.itemByName("pictureLayer").isValid){myDoc[i].layers.add({name:"pictureLayer" })}; 
      if(!myDoc[i].layers.itemByName("textLayer").isValid){myDoc[i].layers.add({name:"textLayer"})}; 
      
      var textLayer = myDoc[i].layers.itemByName("textLayer"); 
      textLayer.locked = false; 
      
      var pictureLayer = myDoc[i].layers.itemByName("pictureLayer"); 
      pictureLayer.locked = false; 
      
      myDoc[i].spreads.everyItem().textFrames.everyItem().locked = false; 
      myDoc[i].spreads.everyItem().textFrames.everyItem().itemLayer = textLayer; 
      
      var myPageItems = myDoc[i].pageItems;  
      
      for(var i=0; i<myPageItems.length; i++){  
          
      if(myPageItems[i].allGraphics.length==1){
          
      myPageItems[i].locked = false; 
      myPageItems[i].itemLayer = pictureLayer; 
      }
      }
      }
      }
      

       

       

      I've been searching all over the forum, gathering some pieces of code but now I'm stuck, and begging for your help !

      Could someone please explain to me what's going on and what's wrong, and/or could get this script to work as it should ?

       

      Thanks everyone !

        • 1. Re: Move graphics and texts to separate layers on multiple opened documents
          Synkx Level 1

          Well, found the answer myself !

           

          I only needed to rename the "var i" in the second "for" of the script.

           

          function moveToLayer(){   

          var myDoc = app.documents;

           

                  for (var i = myDoc.length-1; i >= 0; i--) {

                     

          if(!myDoc[i].layers.itemByName("pictureLayer").isValid){myDoc[i].layers.add({name:"picture Layer" })};

          if(!myDoc[i].layers.itemByName("textLayer").isValid){myDoc[i].layers.add({name:"textLayer" })};

           

          var textLayer = myDoc[i].layers.itemByName("textLayer");

          textLayer.locked = false;

           

          var pictureLayer = myDoc[i].layers.itemByName("pictureLayer");

          pictureLayer.locked = false;

           

          myDoc[i].spreads.everyItem().textFrames.everyItem().locked = false;

          myDoc[i].spreads.everyItem().textFrames.everyItem().itemLayer = textLayer;

           

          var myPageItems = myDoc[i].pageItems; 

           

          for(var k=0; k<myPageItems.length; k++){ 

             

          if(myPageItems[k].allGraphics.length==1){

             

          myPageItems[k].locked = false;

          myPageItems[k].itemLayer = pictureLayer;

          }

          }

          }

          }

           

          Hope this will help someone else !

           

          (I now face another problem, but I may investigate a little more before posting another question ! )

          • 2. Re: Move graphics and texts to separate layers on multiple opened documents
            Peter Kahrel Adobe Community Professional & MVP

            The embeddd for-loop uses the same variable for the counter, i. Use a different variable for the embedded loop:

             

            for(var j=0; j<myPageItems.length; j++){ 

             

            and change all i's to j inside that loop.

             

            Peter

            • 3. Re: Move graphics and texts to separate layers on multiple opened documents
              Synkx Level 1

              Hi Peter,

               

              Thank you for your answer !

              I've been able to find that out by myself, but I may post another question tomorrow.

              Thanks again !