Skip to main content
New Participant
February 16, 2010
Answered

Script: rename layers?

  • February 16, 2010
  • 5 replies
  • 38889 views

Hi, is there a quick way how to renumber or batch rename all layers in a file so they would be named in consequent numbers? Doesn't have to start from exact number, I was wondering if maybe there is some sort of script that would help me with that? Thanks Pavel

This topic has been closed for replies.
Correct answer Muppet Mark

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

          layers.name = layers.length - i;

     }

}

5 replies

Inspiring
February 20, 2016

I read all of the above, but I didn't quite find out if there is a script available to only rename selected layers with the option to add a prefix. This would be a very good script to have in the arsenal Anyone know where to look? Is it difficult to make?

Qwertyfly___
Brainiac
February 22, 2016

I was playing with this mid last year, it may do what you want.

var doc = app.activeDocument, lays = [];

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

    lays.push(doc.layers.name);

}

var w = new Window('dialog',"Layer Name Editor");

var list = w.add('group');

list.orientation = "Column";

var head = list.add('group');

head.alignment = "left";

var p = head.add('statictext', undefined, "Prefix");

var n = head.add('statictext', [0,0,165,20], "            Layer Name");

var s = head.add('statictext', undefined, "Suffix");

var  pre = [], nam = [], suf = [];

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

    newLine(i,"item" + i);

}

function newLine(num,item){

    item = list.add('group');

    item.alignment = "left";

    pre[num] = item.add('checkbox', undefined,"");

    nam[num] = item.add('edittext', [0,0,200,20], lays);

    nam[num].characters = 50;

    suf[num] = item.add('checkbox', undefined, "");

}

var sep1 = list.add("panel");

sep1.alignment = ["fill","fill"];

sep1.minimumSize.height = sep1.maximumSize.height = 2;

var prefixt = list.add('statictext', undefined, "Prefix to add to checked layers");

var prefix = list.add('edittext', [0,0,250,20], "");

var sep2 = list.add("panel");

sep2.alignment = ["fill","fill"];

sep2.minimumSize.height = sep2.maximumSize.height = 2;

var prefixt = list.add('statictext', undefined, "Suffix to add to checked layers");

var suffix = list.add('edittext', [0,0,250,20], "");

var sep3 = list.add("panel");

sep3.alignment = ["fill","fill"];

sep3.minimumSize.height = sep3.maximumSize.height = 2;

var ButtonGroup = w.add("group");

  ButtonGroup.margins = [0,-10,0,-8];

  ButtonGroup.alignment = "right";

  var go = ButtonGroup.add ("button", undefined, "OK");

  var stop = ButtonGroup.add ("button", undefined, "Cancel");

  stop.onClick = function(){

  w.close();

  }

    go.onClick = function(){

        var validatePre = false, validateSuf = false, validateMessage = "";

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

            if(pre.value == true && prefix.text == ""){validatePre = true}

            if(suf.value == true && suffix.text == ""){validateSuf = true}

        }

        if(validatePre == true){validateMessage = "Layers have been marked for Prefix, but no Prefix entered\n"}

        if(validateSuf == true){validateMessage = validateMessage + "Layers have been marked for Suffix, but no Suffix entered"}

        if(validateMessage != ""){

            alert(validateMessage);

        }else{

            w.close();

            goTime();

        }

  }

w.show();

function goTime(){

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

        var na = nam.text;

        var pr = "";

        var su = "";

        if(pre.value == true){pr = prefix.text + " - "}

        if(suf.value == true){su = " - " + suffix.text}

        doc.layers.name = pr + na + su;

    }

}

S.Tyrone_Perry
Inspiring
December 5, 2016

And how might I go about adding a script to that one, to only edit visible layers?

Like inserting this from the comments above?

if (layers.visible == true// <--- Testing .. 1,2,3. 

New Participant
July 13, 2015

Hi I am trying to modify this script by changing the 'ca' to something else more relevant to my project in this case 'PBW'. But I get this error whenever I change it.

could someone tell me why this is happening? I would love to be able to rename layers and change the names according to my project.

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

layers.name = 'ca' + (layers.length - i).toString();

}

}

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

layers.name = ‘PBW’ + (layers.length - i).toString();

}

}

StrongBeaver
Brainiac
July 14, 2015

It would be nice to be able to rename sub-layers with this script

Alan I'd like to see what you come up with; I have many script ideas for Ps and or Illustrator; I'm learning Ps DOM and not going as fast as I had planned

New Participant
July 14, 2015

Hey StrongBeaver, I didn't write this script, just copied it from a post above. I found out what was wrong when I was trying to modify it this afternoon. Text edit was replacing single quotes with curly quotes 'ca' or 'PBW' or 'insert your text here'. I had to turn off smart quotes in text edit and it works perfectly now. If you need layer renaming in photoshop I would suggest using Renamy http://klaia.com/Renamy/ they just updated for PS_2015. Its friggan amazing. Too bad theres not something like Renamy for AI.

Inspiring
July 5, 2012

I'm new to scripting in Illustrator. How do I get this to run? I copy and pasted the code into a text editor and saved it with a .jsx suffix. But nothing happens when I run it in Illustrator CS6.

Larry G. Schneider
Inspiring
July 6, 2012

Does your file structure follow the one outlined in the thread. Here's an example of what will happen with this script.

StrongBeaver
Brainiac
April 17, 2015

Is it possible to add prefix and suffix to the layer name ? Another idea is you select your layers and a dialog pops up and you enter in sequential order the name you want for the layers ?

February 16, 2010

happy to have found this — thanks!!

Inspiring
February 16, 2010

This is straight top level layers only…

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

layers.name = i + 1;

}

}

New Participant
February 16, 2010

Hi Mark,

thanks a lot for your answer, it worked perfectly... just one minor tweaking, would it be possible to start the numbering from the most bottom layer and continue upwards?

Thanks again

Pavel

Muppet MarkCorrect answer
Inspiring
February 16, 2010

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

          layers.name = layers.length - i;

     }

}