13 Replies Latest reply on Jul 24, 2013 8:37 AM by tlumbis

    How to delete layer by name?

    tlumbis

      Hello,

      I currently have a script that deletes unused/blank/empty layers:

       

      var layers = app.activeDocument.layers.everyItem().getElements();

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

        if(layers[i].pageItems.length==0){

          layers[i].remove();

        }

      }

       

      but in addition to unused layers, I need it to delete layers which have a layer name that begins with a bracket "[". Often there are template layers in our workflow which are not needed - they are named differently but always begin with a bracket. Example: [page 2] xxx-xxx-####

       

      I've tried frankensteining in a piece of code that Jarek provided for another script which dealt with swatch names but I'm afraid I'm rather hopeless (I am planning on becoming less hopeless... so maybe you won't always be overcome with dread upon seeing my posts... but that doesn't solve my (or your) problem now).

       

      As always, I appreciate your consideration and help.

        • 1. Re: How to delete layer by name?
          TᴀW Adobe Community Professional & MVP

          Well, the name of the layer is simply myLayer.name, which is a string.

          So to check if that begins with a bracket, simply:

           

          if (myLayer.name[0] == "[")

           

          Ariel

          • 2. Re: How to delete layer by name?
            tlumbis Level 1

            Thanks Ariel. I've tried the script below and I receive an error that "myLayer" is undefined. I'm very much assuming that the error is due to my above referenced hopelessness. Any course correction would be appreciated 

             

            var layers = app.activeDocument.layers.everyItem().getElements();

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

              if(layers[i].pageItems.length==0){

                layers[i].remove();

              }

              if (myLayer.name[0] == "["){

            myLayer.remove();

              }

            }

            • 3. Re: How to delete layer by name?
              TᴀW Adobe Community Professional & MVP

              Sure -- you should substitute myLayer with the variable appropriate to

              your script. So in your case:

               

              if (layers[i].name[0] == "["){

                   layers[i].remove();

              }

               

              is, I think, what you're looking for.

               

              The way your script will now work is that it will delete any layer that

              is empty, and any layer whose name begins with "[", which I think is

              what you want.

               

              Ariel

              • 4. Re: How to delete layer by name?
                tlumbis Level 1

                Strange. The first time I ran the script it deleted the layer which had a name that began with a bracket. Then, gleefully, I tried it again (having replaced the bracket-named layer) and I received an error:

                 

                Error string: Object is invalid

                Source: if (layers[i].name[0] == "["){

                 

                It does this repeatedly... it'll work the first time I've run the script (even in a new document), then I'll either ctrl-z to get the old layer back or I'll manually insert a new layer, and when I run the script again I get that errror. Thoughts?

                 

                var layers = app.activeDocument.layers.everyItem().getElements();

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

                  if(layers[i].pageItems.length==0){

                    layers[i].remove();

                  }

                if (layers[i].name[0] == "["){

                   layers[i].remove();

                }

                }

                • 5. Re: How to delete layer by name?
                  TᴀW Adobe Community Professional & MVP

                  After the first appearance of the line

                   

                  layers[i].remove();

                   

                  try adding the line

                   

                  continue;

                   

                  The reason here may be that if a layer is empty, then it is deleted, and

                  so it's no good trying to access its name since it doesn't exist any

                  more. A continue command will force the loop to move on to the next

                  iteration instead of a erroneously moving on to the next if statement.

                   

                  Ariel

                  • 6. Re: How to delete layer by name?
                    Laubender Adobe Community Professional & MVP

                    Why name[0], not just: name?

                     

                    if (layers[i].name === "["){
                    


                    Uwe

                    • 7. Re: How to delete layer by name?
                      Laubender Adobe Community Professional & MVP

                      Also: I would recommend e.g. myLayers instead of simply layers as variable name!

                       

                      Uwe

                      • 8. Re: How to delete layer by name?
                        TᴀW Adobe Community Professional & MVP

                        name[0] because we only want to test the first character of the name.

                        myLayer.name is a string, so myLayer.name[0] will give the first

                        character of that string.

                         

                        I agree that myLayers is better than layers as a variable name.

                        Otherwise it could cause havoc if you're using a with statement.

                        • 9. Re: How to delete layer by name?
                          tlumbis Level 1

                          Ariel, added the continue; (as in the script below and it solved that error). Thank you so much

                           

                          var layers = app.activeDocument.layers.everyItem().getElements();

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

                            if(layers[i].pageItems.length==0){

                              layers[i].remove();

                          continue;

                            }

                          if (layers[i].name == "["){

                             layers[i].remove();

                          }

                          }

                           

                          Laubender, I tried removing the [0] before I added the "continue;" and it stopped the script from deleting the bracket-named layer (no error message). After I added in the continue, I removed the [0] and the layer was deleted as expected.... I'm not sure why this is.

                           

                          Should I, or should I not keep the [0]?

                           

                          Thanks everybody.

                          • 10. Re: How to delete layer by name?
                            TᴀW Adobe Community Professional & MVP

                            If the layer was deleted without the , that means you probably

                            created a test layer named simply "[". But if the layer was named

                            anything else, it wouldn't be deleted.

                             

                            Keep the !

                             

                            Ariel

                            • 11. Re: How to delete layer by name?
                              tlumbis Level 1

                              You and Laubender both agree that "myLayers" is a more appropriate variable name than just "layers".

                               

                              To define this I should just do this (it seems to function properly...)?:

                               

                              var myLayers = app.activeDocument.layers.everyItem().getElements();

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

                              if(myLayers[i].pageItems.length==0){

                              myLayers[i].remove();

                              continue;

                              }

                              if (myLayers[i].name[0] == "["){

                              myLayers[i].remove();

                              }

                              }

                               

                              I really aprpeciate the help.

                              • 12. Re: How to delete layer by name?
                                TᴀW Adobe Community Professional & MVP

                                Yes, that's right.

                                • 13. Re: How to delete layer by name?
                                  tlumbis Level 1

                                  Thanks so much.