7 Replies Latest reply on Jan 25, 2014 8:23 AM by W_J_T

    Replace layer names in selected layers only

    big_smile Level 1

      I am using this script to find and replace words in layers. (The script only targets particualr words, rather than the whole layer name).

       

      I would like to make it so it targets selected layers only.

       

      I have found this script which loops through selected layers only, but I am not sure how to add the find and replace layer name functioality.

       

      Thanks for any help that can be offered.

        • 1. Re: Replace layer names in selected layers only
          W_J_T Level 4

          In the original script your using look into adding a condition using:

           

          if(layerReferenceString.hasSelectedArtwork == true){

               // relative code

          }

           

          That would offer a way to know if a layer is selected or not.

          • 2. Re: Replace layer names in selected layers only
            big_smile Level 1

            This is my code:

             

            function renameText() {

                      if (app.documents.length == 0) return;

                      var docRef = app.activeDocument;

                      recurseLayers(docRef.layers);

            }

             

             

            if (layerReferenceString.hasSelectedArtwork == true) {

                      renameText();

            }

             

             

            function recurseLayers(objArray) {

                      for (var i = 0; i < objArray.length; i++) {

                                objArray[i].name = objArray[i].name.replace(/\s*OLDTEXT\s*\d*/, 'NEWTEXT');

                                if (objArray[i].layers.length > 0) recurseLayers(objArray[i].layers);

                      }

            }

             

            It says layerReferenceString - not defined.

            How can I define layerReferenceString? I have checked in this guide and layerReferenceString doesn't seem to be a built in function, so I am not sure how to make it recognised.

            • 3. Re: Replace layer names in selected layers only
              W_J_T Level 4

              That was just a "generic reference to show the other part of the code. Similar to saying: something.name = blah; You were targeting layers so I just came up with that as a generic reference for you to replace with the appropriate reference in your code, sorry. It was just a pseudo example to show the hasSelectedArtwork.

              • 4. Re: Replace layer names in selected layers only
                big_smile Level 1

                Looking at this guide, there doesn't seem to be a way to detect selected layers. Does this mean it is not possible? Thanks for any help that can be offered.

                • 5. Re: Replace layer names in selected layers only
                  W_J_T Level 4

                  Correct. Yeah there is no direct way unfortunately (like many things via scripting), thats why I suggested using "hasSelectedArtwork", that would work if you select the layer target when selecting your desired layers to rename.

                  • 6. Re: Replace layer names in selected layers only
                    big_smile Level 1

                    Looking through the guide, it doesn't seem "hasSelectedArtwork", is a built in function either. There only seem to be functions to do with selected texct & objects (but no layers).

                     

                    Are there any tutorials or guides that explain how to target selected layers?

                     

                    Thanks for any help!

                    • 7. Re: Replace layer names in selected layers only
                      W_J_T Level 4

                      big_smile wrote:

                       

                      Looking through the guide, it doesn't seem "hasSelectedArtwork", is a built in function either. Are there any tutorials or guides that explain how to target selected layers?

                       

                      Wrong reference manual, see this one:

                       

                      http://www.adobe.com/content/dam/Adobe/en/devnet/pdf/illustrator/scripting/cs6/Illustrator -Scripting-Reference-JavaScript.pdf

                       

                      Page 91 -- CHAPTER 1: JavaScript Object Reference

                       

                      Layer

                       

                      Property

                      hasSelectedArtwork

                       

                      Value type

                      boolean

                       

                      What it is

                      If true, an object in this layer has been selected; set to false to deselect all objects in the layer.

                       

                       

                      So as I talked about here:

                       

                      W_J_T wrote:

                       

                      Correct. Yeah there is no direct way unfortunately (like many things via scripting), thats why I suggested using "hasSelectedArtwork", that would work if you select the layer target when selecting your desired layers to rename.

                       

                      and...

                      W_J_T wrote:

                       

                      if(layerReferenceString.hasSelectedArtwork == true){

                           // relative code

                      }

                       

                      That would offer a way to know if a layer is selected or not.

                       

                      As far as I know that is the only round about way of knowing if a layer is selected via scripting.