6 Replies Latest reply: Oct 5, 2011 6:56 AM by Michael L Hale RSS

    Select layer by name.

    KevinPowell_Art Community Member

      I need to be able to select a specific layer by name to make active. I cannot find any documentation.

       

      The layer name is always "PDF (Replace Contents...)" - then I'll replace the content with a specified  PDF (constant - naming convention also).

       

      -kp

        • 1. Re: Select layer by name.
          KevinPowell_Art Community Member

          I would like to avoid dialog boxes to make this happen at creation.

          • 2. Re: Select layer by name.
            JJMack Community Member

            Search on getbyname in the scripting guides you will find documentation like this

             

            The collections, as in this example, can be treated as arrays, which is useful for iteration. They also provide

            methods to create their contained objects, and to access them by name:

            var newLayer = activeDocument.artLayers.add(); // Create a new ArtLayer object

            newLayer.name = "My Layer"; // name it for later reference

            ...

            var layerRef = activeDocument.artLayers.getByName("My Layer");

             

             

            so you would code somethinf like this

             

             

            try { var layerRef = activeDocument.artLayers.getByName("PDF");}

            catch(e){

                                alert("Document does not have the Required PDF layer");

                                var abort = true

                      }

            • 3. Re: Select layer by name.
              KevinPowell_Art Community Member

              I had that information but I am trying to make the layer "active".

               

               

              I can do basic actions with what you provided but what I am trying to do is set the layer to active so that other people using the script will not have to manually check to see if it is selected.

              • 4. Re: Select layer by name.
                Paul Riggott Community Member

                Here are a couple of examples...

                 

                 

                activeDocument.activeLayer = activeDocument.artLayers.getByName('LayerName');
                activeDocument.activeLayer = activeDocument.layerSets["LayerSetName"].artLayers.getByName ("LayerName");
                
                
                • 5. Re: Select layer by name.
                  KevinPowell_Art Community Member

                  perfect. Thank you.

                  • 6. Re: Select layer by name.
                    Michael L Hale Community Member

                    If all the layers in the document are unique the function below will make a layer active by name regardless of where it is in the layer stack ( nested in groups or not )

                    function makeActiveLayerByName( lyrname ){
                        var desc = new ActionDescriptor();
                            var ref = new ActionReference();
                            ref.putName( charIDToTypeID( "Lyr " ), lyrname );
                        desc.putReference( charIDToTypeID( "null" ), ref );
                        desc.putBoolean( charIDToTypeID( "MkVs" ), false );
                        executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
                    }