15 Replies Latest reply on May 27, 2017 5:49 AM by pixxxel schubser

    select by layer name

    jeremy0013

      I need to select a specific layer or group by name.  What is the correct syntax to do this with Javascript?

        • 1. Re: select by layer name
          williamadowling Level 4

          var myLayer = app.activeDocument.layers["Layer Name"];

           

          (also you can get sublayers like this:

          var mySubLayer = myLayer.layers["Sub Layer Name"];

          )

           

          var myGroup = myLayer.groupItems["Group Name"];

          • 2. Re: select by layer name
            jeremy0013 Level 1

            Thanks for the reply W.  So I am new at this.  Is this var line supposed to select "Layer Name" or does it need to be called from selection.  I know, super basic but I am working on my skills.

            • 3. Re: select by layer name
              jeremy0013 Level 1

              Ok scratch that.  I got it figured out.  One more question.  I need to select the pieces on the layer.  This only selects the layer it self.

              • 4. Re: select by layer name
                williamadowling Level 4

                you want to select all of the items on the layer? or just certain kinds (i.e. text frames, groups, paths)?

                 

                Let's look at both.

                 

                we'll start by selecting all the items on a given layer (in this case the layer is called "Target Layer");

                note that pageItems means any art item in the document. could be text, could be a path, could be a group, could be a compound path.

                 

                var docRef = app.activeDocument;
                var layers = docRef.layers;
                var myLayer = layers["Target Layer"]; //this defines the layer that you want to get the selection from
                
                docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.
                for(var a=0;a<myLayer.pageItems.length;a++){ //here we are looping through each pageItem of myLayer.
                     var currentItem = myLayer.pageItems[a];
                     currentItem.selected = true;
                }
                
                

                 

                now lets do the same thing but we'll just select the groupItems

                 

                var docRef = app.activeDocument;
                var layers = docRef.layers;
                var myLayer = layers["Target Layer"];
                
                docRef.selection = null;
                for(var a=0;a<myLayer.groupItems.length;a++){
                     var currentGroup = myLayer.groupItems[a];
                     currentGroup.selected = true;
                }
                
                

                 

                and finally, for the sake of being thorough, lets select two different types of art items, but not all types. For example, if one layer has text frames, groups, paths and compound paths. Let's just select the textFrames and groupItems. this time we'll have to include a conditional statement to find the correct types of items.

                 

                var docRef = app.activeDocument;
                var layers = docRef.layers;
                var myLayer = layers["Target Layer"];
                
                docRef.selection = null;
                for(var a=0;a<myLayer.pageItems.length;a++){
                     var currentItem = myLayer.pageItems[a];
                     if(currentItem.typename == "TextFrame" || currentItem.typename == "GroupItem"){
                          currentItem.selected = true;
                     }
                }
                
                
                • 5. Re: select by layer name
                  jeremy0013 Level 1

                  Holy Smokes.  This is more than I could have asked for.  Thanks so much Will.

                  • 6. Re: select by layer name
                    williamadowling Level 4

                    You got it brother. Come back any time. I learned pretty much everything i know from other helpful people on this forum. Only makes sense to pay it forward.

                     

                    good luck. =)

                    • 7. Re: select by layer name
                      PrepressPro1 Adobe Community Professional

                      When I try to select on a SubLayer by adding the one line I get MyLayer is undefined.

                       

                      Screen Shot 2017-05-26 at 10.51.04 AM.png

                       

                      Here is what my addition looks like.

                       

                       

                      var docRef = app.activeDocument; 
                      var layers = docRef.layers; 
                      var mySubLayer = myLayer.layers["CUTTER"]; //this defines the Sublayer that you want to get the selection from 
                      docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want. 
                      for(var a=0;a<myLayer.pageItems.length;a++){ //here we are looping through each pageItem of myLayer. 
                           var currentItem = myLayer.pageItems[a]; 
                           currentItem.selected = true; 
                      • 8. Re: select by layer name
                        williamadowling Level 4

                        Is that the entire code you're using or just a part?

                         

                        if that's the whole code, then you've never declared what "myLayer" should be. You have "layers" defined, but you never said which element of the layers array should be equal to "myLayer"

                         

                        If there's more code, can you post the entire script?

                        • 9. Re: select by layer name
                          PrepressPro1 Adobe Community Professional

                          This is the entire script. I simply used the first script in this thread which selects all items on a layer and used the additional information in the second post which is said to allow you to target a Sub Layer rather then a Layer.

                          • 10. Re: select by layer name
                            williamadowling Level 4

                            Ah, very good then. you can simply remove any reference to myLayer, assuming that "CUTTER" is a top level layer. try this:

                             

                            var docRef = app.activeDocument;

                            var layers = docRef.layers;

                            var mySubLayer = layers["CUTTER"]; //this defines the Sublayer that you want to get the selection from

                            docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.

                            for(var a=0;a<mySubLayer.pageItems.length;a++){ //here we are looping through each pageItem of myLayer.

                                var currentItem = myLayer.pageItems[a];

                                currentItem.selected = true;

                            }

                            • 11. Re: select by layer name
                              pixxxel schubser Level 5

                              PrepressPro1

                              Normally it is better to open a new thread for a new question (and if necessary to link to the old topic).

                               

                              But anyway.

                              I think this could be a better way

                              var aDoc = app.activeDocument;
                              aDoc.selection = null;
                              try {
                                  // be sure that top level layer with name "TopLayer" with sublayer with name "CUTTER" exists
                                  var myLayer = aDoc.layers.getByName('TopLayer').layers.getByName('CUTTER');
                                  myLayer.hasSelectedArtwork = true;
                                  }
                              catch (e) {
                                  alert("the layer doesn't exist");
                                  // do whatever you want
                                  }
                              

                               

                              Have fun

                               

                              • 12. Re: select by layer name
                                PrepressPro1 Adobe Community Professional

                                Not getting it at all. Trying to select all objects on a sub layer called CUTTER. Now I'm getting No Such Element Line 5.

                                 

                                 

                                Screen Shot 2017-05-26 at 12.50.34 PM.png

                                • 13. Re: select by layer name
                                  williamadowling Level 4

                                  best to heed Pixxxel's advice and create a new discussion. Try to add as much information as possible and be specific so we can provide adequate help. Otherwise we're just kind of shooting in the dark and then trying to debug a system we don't understand.

                                   

                                  Screenshots of the layers panel with the desired sublayer highlighted would be a great help.

                                   

                                  I'll see you in the new discussion.

                                  • 14. Re: select by layer name
                                    PrepressPro1 Adobe Community Professional

                                    Sorry about hijacking the thread. There is a sublayer in my document called CUTTER and I have named the parent layer TopLayer but the script returns the alert the layer doesn't exist.

                                    • 15. Re: select by layer name
                                      pixxxel schubser Level 5

                                      PrepressPro1,

                                      is this "sublayer" really a sublayer? Or a grouped item instead?

                                       

                                      Like williamadowling said before: "… Try to add as much information as possible and be specific so we can provide adequate help. Otherwise we're just kind of shooting in the dark and then trying to debug a system we don't understand.

                                       

                                      Screenshots of the layers panel with the desired sublayer highlighted would be a great help …"