4 Replies Latest reply on Jul 3, 2013 1:23 PM by CarlosCanto

    script to rename layers alphabetically

    MMScreenX

      Hi everyone,

       

      I have a renaming script that works only on numbers and I want to make it work for letters also.

       

      What I`m trying to achieve is to select some layers --> load the script --> enter the desired name of the layer and in brackets insert the letter from where to start. Then the script iterates through the alphabet renaming the layers and adding the coresponding letter to the name and removing the brackets. For example, I select 4 layers, load the script and when the prompt comes on... enter the name:      test[C] . At the end I should have the layers renamed testC,testD,testE,testF

       

      Any help would be appreciated!

       

      Thanks

        • 1. Re: script to rename layers alphabetically
          Muppet Mark Level 5

          Script has NO access to which layers you have selected in the layers palette…

          • 2. Re: script to rename layers alphabetically
            MMScreenX Level 1

            What about the sub-layers then?

             

            This script does what I need but for numbers only.

             

             

            var doc = app.activeDocument,

                sel = doc.selection,

                rename = prompt("Enter the name:",""),

                done = 0;

             

             

            if(sel.length>0){

              var nr=rename.match(/\[\w+\]/);

              if(nr){ nr=nr[0].match(/\w+/)[0];

              }

              for(var z=0;z<sel.length;z++){   

                if(nr){

                    theRenaming=rename.replace(/\[\w+\]/,nr);

                    sel[z].name = theRenaming;

                    nr++;

                } else

                  sel[z].name = rename

              }

            } else

              alert("Nothing was selected")

            • 3. Re: script to rename layers alphabetically
              MMScreenX Level 1

              Ok, I've managed to rename the layers based on selection but I've encountered another problem.

              If I select 4 layers for example and run the script starting from letter "A"..it goes well..it renames the four layers from A to D. My problem now is that if I want to start renaming those 4 layers from letter B or C, it will skip layer 1 (if letter B) or layer 2(if letter C). So basically there should be a way to reset the counter when starting to rename and rename first letter with my specified letter and iterate.

               

              Here is my code so far:

               

               

              var doc = app.activeDocument,

              sel = doc.selection,

              rename = prompt("Enter the name:","");

              done = 0;

               

              var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

              var r = /\[\w+\]/i;

              var matching = rename.match( r );

               

              var letter = str.search( matching );

               

              if (letter>=0)

              {

                 for(i=letter; i<sel.length;i++)

                 {

               

                      var nextChar = str.charAt(i);

               

                    if(nextChar){

                          theRenaming=rename.replace(/\[\w+\]/, nextChar);

                          sel[i].name = theRenaming;

                          letter++;

                      }

                      else

                      sel[i].name = rename;

                 }   

               

              } else

              {

              alert("Nothing Found!");

              }

               

              Any help would be appreciated.

              I still don't know why muppet mark said that you cannot access layer names.

              • 4. Re: script to rename layers alphabetically
                CarlosCanto Adobe Community Professional & MVP

                so, what you wanted is to rename Selected Objects, not Layers, right?

                 

                check this script, I took a different route, I added a couple of comments, if you have any trouble with it, feel free to ask

                 

                // this script Renames selected Objects with a base name plus an incremental suffix
                // Carlos Canto // 07/03/2013;
                // http://forums.adobe.com/thread/1243774?tstart=0
                
                
                var doc = app.activeDocument;
                var sel = doc.selection;
                var rename = prompt("Enter the name and ONE character suffix (comma separated, no space):","test,A", "Increment Rename Selected Objects"); 
                if (rename!=null) {// quit if pressed Cancel
                    var nameArray = rename.split(","); 
                
                
                    var base = nameArray[0];
                    var suffix = nameArray[1];
                
                
                    var charCode = suffix.charCodeAt (); // convert the suffix character to Unicode
                
                
                    if(sel.length>0){
                        for(var z=0;z<sel.length;z++) {   
                            var suffix = String.fromCharCode (charCode+z); // increase the Unicode value and convert it back to a String
                            sel[z].name = base + suffix;
                        }
                
                
                    // Illustrator bug? new names don't update in the UI, this will force the layer panel to redraw
                    sel[0].selected = false;
                    sel[0].selected = true;
                
                
                    }
                    else
                      alert("Nothing was selected");
                }
                else alert("Cancelled by User");