Skip to main content
Participating Frequently
February 12, 2015
Answered

script to batch rename/number layers in Illustrator CS6

  • February 12, 2015
  • 1 reply
  • 3513 views

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;
var newNum=i+1;
currLayer.name="Layer "+newNum;
subLayers=topLayers.layers;
for(j=0;j<subLayers.length;j++){
  var currSubLayer=subLayers;
  var newSubNum=j+1;
  currSubLayer.name="Layer "+ newNum+"."+newSubNum;
  subSubLayers=subLayers.layers;
   for(k=0;k<subSubLayers.length;k++){
    var currSubSubLayer=subSubLayers;
    var newSubSubNum=k+1;
    currSubSubLayer.name="Layer "+ newNum+"."+newSubNum+"."+newSubSubNum;
   }
  }
}

////END SCRIPT////

This topic has been closed for replies.
Correct answer Silly-V

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.selected == true)

var newNum=i+1;      

       {

               docRef.pageItems.name = "applyColor"+newNum;;

       }

}


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;

1 reply

Silly-V
Legend
February 12, 2015

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;

        var newNum=i+1;

        currLayer.name="Layer "+newNum;

        var subLayers=topLayers.layers;

        for(j=0;j<subLayers.length;j++){

            var currSubLayer=subLayers;

            var newSubNum=j+1;

            currSubLayer.name="Layer "+ newNum+"."+newSubNum;

            subSubLayers=subLayers.layers;

            for(k=0;k<subSubLayers.length;k++){

                var currSubSubLayer=subSubLayers;

                var newSubSubNum=k+1;

                currSubSubLayer.name="Layer "+ newNum+"."+newSubNum+"."+newSubSubNum;

           }

        }

    }

}

test();

Participating Frequently
February 12, 2015

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

Silly-V
Legend
February 12, 2015

Can you attach a screenshot of your layers palette?