12 Replies Latest reply on Feb 8, 2016 6:28 PM by G-Whizz

    script to batch rename/number layers in Illustrator CS6

    keelercreative

      Is there a script that I can use in Illustrator to batch rename all sub-layers? I would also like to be able to add sequential numbers to each layer too. I saw an older post that contained a script to do this but it seemed outdated and doesn't work on CS6. Here it is for reference, maybe someone can modify it to work for CS6?

       

      ////START SCRIPT////

      docRef=app.activeDocument;
      topLayers=docRef.layers;
      for(i=0;i<topLayers.length;i++){
      var currLayer=topLayers[i];
      var newNum=i+1;
      currLayer.name="Layer "+newNum;
      subLayers=topLayers[i].layers;
      for(j=0;j<subLayers.length;j++){
        var currSubLayer=subLayers[j];
        var newSubNum=j+1;
        currSubLayer.name="Layer "+ newNum+"."+newSubNum;
        subSubLayers=subLayers[j].layers;
         for(k=0;k<subSubLayers.length;k++){
          var currSubSubLayer=subSubLayers[k];
          var newSubSubNum=k+1;
          currSubSubLayer.name="Layer "+ newNum+"."+newSubNum+"."+newSubSubNum;
         }
        }
      }

      ////END SCRIPT////

        • 1. Re: script to batch rename/number layers in Illustrator CS6
          Silly-V Adobe Community Professional

          It didn't look like it wouldn't work, to me at a glance, except for a couple missing var keywords for best practice.

          Maybe you were no targeting the appropriate app?

           

          Try this:

          #target illustrator
          function test(){
              var docRef=app.activeDocument;
              var topLayers=docRef.layers;
              for(i=0;i<topLayers.length;i++){
                  var currLayer=topLayers[i];
                  var newNum=i+1;
                  currLayer.name="Layer "+newNum;
                  var subLayers=topLayers[i].layers;
                  for(j=0;j<subLayers.length;j++){
                      var currSubLayer=subLayers[j];
                      var newSubNum=j+1;
                      currSubLayer.name="Layer "+ newNum+"."+newSubNum;
                      subSubLayers=subLayers[j].layers;
                      for(k=0;k<subSubLayers.length;k++){
                          var currSubSubLayer=subSubLayers[k];
                          var newSubSubNum=k+1;
                          currSubSubLayer.name="Layer "+ newNum+"."+newSubNum+"."+newSubSubNum;
                     }
                  }
              }
          }
          
          
          test();
          
          
          
          • 2. Re: script to batch rename/number layers in Illustrator CS6
            keelercreative Level 1

            It doesn't work for any sub-layers, but it actually does rename the top layers. So somewhere after the line "var subLayers=topLayers[i].layers;" doesn't work.

            • 3. Re: script to batch rename/number layers in Illustrator CS6
              Silly-V Adobe Community Professional

              Can you attach a screenshot of your layers palette?

              • 4. Re: script to batch rename/number layers in Illustrator CS6
                keelercreative Level 1

                You got me to investigate if other files and layer structures work, and I found one that partially worked. It stopped working after renaming the first top layer and sub-layers within that.

                Here's what kind of worked:

                before: Screen Shot 2015-02-12 at 1.35.23 PM.png after: Screen Shot 2015-02-12 at 1.42.10 PM.png

                 

                Here's the structure that I ultimately want to edit and rename. They are the end levels, sub-sub-sub layers. I want all of them to be renamed to "applyColor1" "applyColor2" etc.

                Screen Shot 2015-02-12 at 2.11.40 PM.png

                • 5. Re: script to batch rename/number layers in Illustrator CS6
                  Silly-V Adobe Community Professional

                  This is odd, I am not sure why it would stop and not finish.  I have it working properly on CC2014, Windows.

                  Did you notice any strange things while the script was running?

                  • 6. Re: script to batch rename/number layers in Illustrator CS6
                    keelercreative Level 1

                    Definitely odd. Nothing strange happened when I ran the script - it ran and finished with no warnings.

                    I noticed another thing. It seems the script works on layers that allow creating a sublayer and make/release clipping masks (icons found on the bottom of the layers panel). Where for the layers that don't rename, those icons are grayed out. What does that mean?

                    • 7. Re: script to batch rename/number layers in Illustrator CS6
                      keelercreative Level 1

                      OK I think I figured out the difference - the ones that don't work with the script are maybe not even technically layers, they are components or objects of layers. With that said, do you know how to rename those component names??

                      • 8. Re: script to batch rename/number layers in Illustrator CS6
                        Silly-V Adobe Community Professional

                        I am not sure of what you mean in "components of layers".  But, I have seen some strange stuff that hardly ever comes up, so I am curious...  By chance, was the file originated from other software?

                        I just don't see a reason for what I am seeing with my own eyes in your screenshot.

                        • 9. Re: script to batch rename/number layers in Illustrator CS6
                          keelercreative Level 1

                          I figured it out. They are just objects and paths, not layers (even though you can order them like layers), so that's why the script doesn't work with them. I found another thread that renames (selected) objects, so I combined with this one to add the incremental numbering. My only remaining question is how do you start numbering at "0" and not "1"?

                           

                           

                          //how to use: select objects that you want to rename, then run script

                          //note - new name may not display until you unselect objects

                           

                          var docRef = activeDocument;

                           

                           

                          for (var i=0; i < docRef.pageItems.length; i++)

                          {

                                 if (docRef.pageItems[i].selected == true)

                           

                          var newNum=i+1;      

                                 {

                                         docRef.pageItems[i].name = "applyColor"+newNum;;

                                 }

                          }

                          • 10. Re: script to batch rename/number layers in Illustrator CS6
                            Silly-V Adobe Community Professional

                            So those were not even layers, but they looked like they were layers? I thought they were layers because it didn't look like they were inside any layer.. but it may have been just the screenshot?  But, I did consider this and dismissed this possibility when first looking at this.

                            To start at 0, where it says var newNum = i+1, just change to var newNum = i;

                            • 11. Re: script to batch rename/number layers in Illustrator CS6
                              keelercreative Level 1

                              Yes, they were just objects and paths. Thanks for the numbering change - it worked!

                              • 12. Re: script to batch rename/number layers in Illustrator CS6
                                G-Whizz

                                Hi mate,

                                 

                                I've spent a few days trawling to find what you seem to be close to setting out here. Unfortunately that script works on Paths or Objects that are selected or not selected, which is not helpful. Even though the script says "if (docRef.pageItems[i].selected == true)". This poses a real problem, because as a user, I ONLY want it to work on selected objects. I modifiers your script to change names like this:

                                 

                                var docRef = activeDocument;

                                  for (var i=0; i < docRef.pageItems.length; i++)

                                {

                                       if (docRef.pageItems[i].selected == true)

                                var newNum=i+1;     

                                       {

                                               docRef.pageItems[i].name = i + 1;

                                       }

                                }

                                 

                                This then just gives a numerical sequence (without prefixes) to ALL Paths/Objects within the file. I tried changing the == to just = in the hope this would work. Alas not. Any ideas would be most appreciated.

                                 

                                I'm working a plan of an airport car park containing over 3000 spaces and each path (each space) needs to be numbered for export to SVG files. Having just complete one with manually re-naming every darn single space with a sequential number, which took hours to get right, I desperately need a script that works. I just want to renumber JUST selected Paths/Objects, not all of them. I don't even know if that's possible.

                                 

                                Cheers.