12 Replies Latest reply on Jun 10, 2015 2:23 AM by Qwertyfly...

    Find a specific Sublayer and move to top level layer

    subieguy2 Level 1

      I have a lot of illustrator files that are organized by a template size. The layers are setup so that the top level layer is always Template. However the sublayer I need to move to the top level could be called 42 Pg Border or 30 Pg Border or 24 Pg Border.

      Is there a way for a script to search through the layers and if it finds "42 Pg Border" or "30 Pg Border" or "24 Pg Border" in the sublayer Template it would move it to the top level but if it doesn't see one of those it doesn't give an error?

       

      I'm still new to scripting so the extent of my knowledge on this is to scan all the layers and unlock them. Then look for a specific layer name. It's the sublayer and moving it to the top level that I am not sure of.

       

      Here is my current code:

      var doc = app.activeDocument;
      var allLayers = doc.layers;
      for (var i = allLayers.length-1; i >= 0; i--){
          allLayers[i].locked = false;
          if (allLayers[i].name == "42 Pg Border" || allLayers[i].name == "30 Pg Border" || allLayers[i].name == "24 Pg Border") {
              alert("I found the " + allLayers[i].name + " layer")
          } else {
              alert("Not the right layer")
          }
      }
      

       

      Any help would be greatly appreciated!

      I am using CS4 on a Windows 64 Bit PC with JavaScript

        • 1. Re: Find a specific Sublayer and move to top level layer
          Qwertyfly... Level 4

          Try This...

          var doc = app.activeDocument;  
          var allLayers = doc.layers; 
          var count = 0; 
          for (var i = allLayers.length-1; i >= 0; i--){  
              allLayers[i].locked = false;  
              if (allLayers[i].name == "42 Pg Border" || allLayers[i].name == "30 Pg Border" || allLayers[i].name == "24 Pg Border") {  
               allLayers[i].zOrder(ZOrderMethod.BRINGTOFRONT);
                  ++count;
              }
          }
            if (count == 0){
               alert("No Border Layer Found");
            }
          
          • 2. Re: Find a specific Sublayer and move to top level layer
            subieguy2 Level 1

            I get the alert for No Border Layer Found because it is in a sublayer. The code above only looks through all the layers at the top level.

            • 3. Re: Re: Find a specific Sublayer and move to top level layer
              Qwertyfly... Level 4

              Sorry, I missed the mention of sublayers. even though it was the whole question... OOPS.

               

              Try this.

               

              var doc = app.activeDocument;  
              var allLayers = doc.layers; 
              var count = 0; 
              for (var i = allLayers.length-1; i >= 0; i--){
                  deeper(allLayers[i]);
              }
                if (count == 0){
                   alert("No Border Layer Found");
                }  
              function checkLayer(parent,layer){
                layer.locked = false;  
                  if (layer.name == "42 Pg Border" || layer.name == "30 Pg Border" || layer.name == "24 Pg Border") {  
                layer.move(allLayers[0],ElementPlacement.PLACEBEFORE);
                      ++count;
                  }
              }
              function deeper(parent){
              var subLayers = parent.layers;
                if (subLayers.length > 0){
                for (var i = subLayers.length-1; i >= 0; i--){  
                checkLayer(parent,subLayers[i]);
                if (count>0){return}
                deeper(subLayers[i]);
                }
                } return
              }
              
              • 4. Re: Re: Re: Find a specific Sublayer and move to top level layer
                subieguy2 Level 1

                OK so that code is really close! I tweaked it a bit. I kept getting an error that said cannot move a locked layer. So I just made a loop to unlock ALL the layers first thing. Then to get rid of some of the "extra" code I wrote I changed this line:

                if (layer.name == "42 Pg Border" || layer.name == "30 Pg Border" || layer.name == "24 Pg Border") { 
                

                to this.....

                if ((layer.name).substr(-6) == "Border") { 
                

                So now instead of looking for 42 30 24 (and other border templates I may have to add) I just told it to look at the 6 characters at the end of the string (which will always be the word Border).

                 

                OK so now the code runs and does what it is supposed to but I am getting an undefined in my javascript console in extendscript. Any ideas?

                Current updated code:

                var doc = app.activeDocument;    
                var allLayers = doc.layers;   
                var count = 0;
                
                
                for (var z = allLayers.length-1; z >= 0; z--){
                    allLayers[z].locked = false;
                  }
                
                
                for (var i = allLayers.length-1; i >= 0; i--){  
                deeper(allLayers[i]);
                
                }  
                  if (count == 0){  
                     alert("No Border Layer Found");  
                  }    
                function checkLayer(parent,layer){     
                    if ((layer.name).substr(-6) == "Border") {    
                  layer.move(allLayers[0],ElementPlacement.PLACEBEFORE);  
                        ++count;  
                    }  
                }  
                function deeper(parent){  
                var subLayers = parent.layers;  
                  if (subLayers.length > 0){  
                  for (var i = subLayers.length-1; i >= 0; i--){    
                  checkLayer(parent,subLayers[i]);  
                  if (count>0){return}  
                  deeper(subLayers[i]);  
                  }  
                  } return  
                }
                
                • 5. Re: Find a specific Sublayer and move to top level layer
                  subieguy2 Level 1

                  Is there a way to script something that will just move ANY/ALL sublayers to a top level layer?

                  That may work more efficiently for me as the more jobs I get the less consistency I am noticing.

                  One file has the following layer:

                  30 Page

                       > 30 Pg Border

                  Other files have been formatted:

                  Template Layers

                       > 42 Page

                            > 42 Pg Border

                  • 6. Re: Re: Find a specific Sublayer and move to top level layer
                    Qwertyfly... Level 4

                    give this a shot.

                    its still early and my brain is not fully functioning yet.

                     

                    bringing all layers to top will (may) mess up stacking order alot.

                     

                    var doc = app.activeDocument;      
                    var allLayers = doc.layers;       
                      
                    for (var i = allLayers.length-1; i >= 0; i--){ 
                      allLayers[i].locked = false;   
                      deeper(allLayers[i]);  
                    }    
                              
                    function deeper(parent){    
                    var subLayers = parent.layers;    
                      if (subLayers.length > 0){    
                      for (var i = subLayers.length-1; i >= 0; i--){
                      subLayers[i].locked = false;
                      deeper(subLayers[i]);
                      if ((subLayers[i].name).substr(-6) == "Border") {
                         subLayers[i].move(allLayers[0],ElementPlacement.PLACEBEFORE);
                      }
                      }    
                      } 
                    }
                    
                    • 7. Re: Re: Re: Find a specific Sublayer and move to top level layer
                      subieguy2 Level 1

                      That works perfect! Thank you again for your help!

                       

                      You helped me with this script and my other one. As a thank you I would like to offer something to you. Nothing big but just something to show my gratitude....

                       

                      I'll message you.

                      • 8. Re: Re: Re: Find a specific Sublayer and move to top level layer
                        Qwertyfly... Level 4

                        I don't require anything, the thanks is good for me.

                        I'm glad I could help you out.

                        Let me know if the scripts need any refinement.

                        especially the save script.

                        there should be some safe guard like:

                        if folder does not exist alert.

                        in case the file name has a typo etc...

                         

                        I enjoy nutting out simple programming tasks.

                        and doing it for others gives me ideas on speeding up my own workflow and improving my own scripts.

                         

                        Thanks for the offer though...

                        • 9. Re: Re: Find a specific Sublayer and move to top level layer
                          suneelkv Level 1

                          Hi QwertyFly,

                           

                          I've a requirement something similar to this.

                          I've some text frames inside sublayers.

                           

                          Main Layer 1

                               Sublayer 1

                               Sublayer 2

                                    Sub sub layer 1

                                    Sub sub layer 2

                               Sublayer 3

                                    Sub sub layer 1

                                    Sub sub layer 2

                           

                                              Sub sub sub  layer 1

                                              Sub sub sub layer 2

                           

                               Sublayer 4

                               .......

                          I should traverse into sub and all the sublayers inside sublayers and find the text frames available in those and get those.

                          I could able to get only to one level of sublayer.

                          Please see code


                          ****

                                 var doc = activeDocument;

                                 var totalLayers = doc.layers.length;  

                                $.writeln("Print the total layers available in the document:::",totalLayers);

                                for(var i = 0; i < app.documents.length; i++)

                                {   

                                         for ( var i = 0 ; i < totalLayers ; i++)

                                         {

                                                      var currentLayer = doc.layers[i];

                                                        $.writeln("currentLayer.name :::PRINT:::",currentLayer.name);

                                                          var subLayers = doc.layers[i].layers.length;         

                                                          $.writeln("Print the total subLayers available in the document:::",subLayers);

                                                             for(var k = 0; k < doc.layers[i].layers.length;k++)

                                                             {

                                                                    $.writeln("Total Text Frames available in the sublayer::::",doc.layers[i].layers[k].textFrames.length);

                                                                   for(var j = 0; j < doc.layers[i].layers[k].textFrames.length; j++)

                                                                   {

                                                                  var str = doc.layers[i].layers[k].textFrames[j].contents;   

                                                                  $.writeln("Text :::",str," ::Size :::",doc.layers[i].layers[k].textFrames[j].textRange.characterAttributes.size," ::Text Font :::",

                                                                  doc.layers[i].layers[k].textFrames[j].textRange.characterAttributes.textFont);

                           

                                                                  var haystack = doc.layers[i].layers[k].textFrames[j].textRange.characterAttributes.textFont; 

                                                                  var textSize= doc.layers[i].layers[k].textFrames[j].textRange.characterAttributes.size;

                           

                                                                  $.writeln("doc.layers[i].layers[k].textFrames[j].anchor[0] :::",doc.layers[i].layers[k].textFrames[j].anchor[0]);

                                                                  $.writeln("doc.layers[i].layers[k].textFrames[j].anchor[1] :::",doc.layers[i].layers[k].textFrames[j].anchor[1]);

                           

                                                          }          

                                                      }

                                                     

                          ****

                          Please direct me in the right way....



                          • 10. Re: Re: Find a specific Sublayer and move to top level layer
                            Qwertyfly... Level 4

                            do you need to go through each layer and sublayer?

                            just access the textFrames array.

                             

                            var doc = app.activeDocument;
                            var texts = doc.textFrames;
                            var textsQty = texts.length;
                            var text = [], position = [];
                            for(var i = 0; i < textsQty; i++){
                                text[i] = texts[i].contents;
                                position[i] = [];
                                position[i][0] = texts[i].top;
                                position[i][1] = texts[i].left;
                                alert("Frame" + i + "\nPosition (" + position[i][0] + "," + position[i][1] + ")\nContents:\n" + text[i]);
                            }
                            

                             

                            ps. cant use anchor on most text frames...

                            • 11. Re: Re: Find a specific Sublayer and move to top level layer
                              suneelkv Level 1

                              Vow!!!! thats amazing...Lines of code are very less....

                              Yeah I have used the code above and able to traverse through each layer and sublayer.

                               

                              Many thanks for ur timely help Qwertyfly.