• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Script: rename layers?

Community Beginner ,
Feb 15, 2010 Feb 15, 2010

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

33.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Feb 16, 2010 Feb 16, 2010

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

          layers.name = layers.length - i;

     }

}

Votes

Translate

Translate
Adobe
Guru ,
Feb 16, 2010 Feb 16, 2010

Copy link to clipboard

Copied

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;

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 16, 2010 Feb 16, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 16, 2010 Feb 16, 2010

Copy link to clipboard

Copied

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

          layers.name = layers.length - i;

     }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 16, 2010 Feb 16, 2010

Copy link to clipboard

Copied

Thanks a lot, Mark!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2010 Mar 03, 2010

Copy link to clipboard

Copied

Thank you for the script. It is very useful.

Could you help me with one thing? If I want to add letters "ca" in front of the number, where should I add "ca" and how to do that?

The present script is 1, 2, 3, 4... on each layer.

I want the script to be ca1, ca2, ca3, ca4... on each layer.

Please help me out.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

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

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

I have a script that exports each top layer to a JPG, while all layers named ALWAYS are combined with it (for webdesign screens, the ALWAYS layer usually contains a browser background image, main menu, and other recurring elements).

http://supermetricity.com/2009/02/11/save-layers-as-jpgs-from-illustrator/

I've been trying to combine the rename script with my export script, but with little success.

Ideally, the export script would name the jpgs it makes from the top layers from the bottom up, adding numbers "01" +n befroe the original layer name, and the layer(s) called ALWAYS would not be exported as single JPG at all...

if that's too complex, is there a way to just not rename any layers called ALWAYS in your rename script?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

Not too sure if this is what you meant or if this is the best way but I tried…

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

var myNumber = 1;

for (var i = layers.length-1; i >= 0; i--) {

if (layers.name != 'ALWAYS') {

layers.name = myNumber + 'n ' + layers.name;

myNumber = myNumber + 1;

continue;

}

}

myNumber = 1;

}

Should number from bottom up skipping layers names 'ALWAYS'

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

fantastic, thanks so much!

that's what I actually tweaked it to now:

#target illustrator

var docRef = app.activeDocument;

with (docRef) {
    var myNumber = 1;
    for (var i = layers.length-1; i >= 0; i--) {
        if (layers.name != 'ALWAYS') {    
                layers.name = myNumber + '-' + activeDocument.name;
                myNumber = myNumber + 1;
                continue;
        } 
    }
    myNumber = 1;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

hmmmm. sorry to bother again — is there any way to add a leading zero to the ones below ten, i.e. creating a double digit number?

thanks a million

m

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 05, 2010 Mar 05, 2010

Copy link to clipboard

Copied

docRef is a reference to the activeDocument so while we are inside of the with (docRef) {doStuffHere} you don't need to mention it again… Just use 'name'. This should check the layers length as string to count the required zero padding…

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

var myNumber = 1;

for (var i = layers.length-1; i >= 0; i--) {

var x = layers.length.toString().length;

var z = zeroPad(myNumber, x);

if (layers.name != 'ALWAYS') {    

layers.name = z + '-' + name;

myNumber = myNumber + 1;

continue;

}

// myNumber = 1;

}

function zeroPad(num, digit) {

   var tmp = num.toString();

   while (tmp.length < digit) {tmp = '0' + tmp;}

   return tmp;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 05, 2010 Mar 05, 2010

Copy link to clipboard

Copied

Thanks Mark!

works perfect now, I had to make a small adjustment though, and set digit to 2...see below:

#target illustrator

var docRef = app.activeDocument;

with (docRef) {
    var myNumber = 1;
    for (var i = layers.length-1; i >= 0; i--) {
        var x = layers.length.toString().length;
        var z = zeroPad(myNumber);
        if (layers.name != 'ALWAYS') { 
            if (layers.name != 'GRID') {  
                layers.name = z + '-' + name;
                myNumber = myNumber + 1;
                continue;
            }            
        }
    }
    // myNumber = 1;
}

function zeroPad(num) {
    var tmp = num.toString();
    while (tmp.length < 2) {tmp = '0' + tmp;}
    return tmp;
}
       

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 06, 2010 Mar 06, 2010

Copy link to clipboard

Copied

Then you don't need this line… 'var x = layers.length.toString().length;' You have hardcoded the length so you won't get 001, 011, 123 if the number of layers increases beyond 99.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 06, 2010 Mar 06, 2010

Copy link to clipboard

Copied

Then you don't need this line… 'var x = layers.length.toString().length;' You have hardcoded the length so you won't get 001, 011, 123 if the number of layers increases beyond 99.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 16, 2017 Aug 16, 2017

Copy link to clipboard

Copied

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

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

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

}

}

wow thanks, that really helped me out..
but i was wondering there must be a way to get input dialogue window so user can insert prefix each time the script is run. currently i have to change it each time from notepad when script is run.
I am not a programmer, but in python there is a command "input" and "raw_input", value is stored in this variable and later that variable is used.

can somebody help me out?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 16, 2017 Aug 16, 2017

Copy link to clipboard

Copied

Hi! Hopefully this suggestion will solve your issue.

use a simple prompt:

#target illustrator

function test(){

  var input = prompt("Enter info", "");

  if(input){

    alert(input);

  }

};

test();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 16, 2017 Aug 16, 2017

Copy link to clipboard

Copied

thanks, combining with previous version it worked

#target illustrator

var input = prompt("Enter Layer Name", "");

var docRef = app.activeDocument;

with (docRef) {

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

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

     }

}

alert("Layers renamed");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 28, 2010 Oct 28, 2010

Copy link to clipboard

Copied

thanks to everyone for the help so far.  these scripts are very helpful.

is there any way that i can limit the script to selected layers only?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2010 Oct 29, 2010

Copy link to clipboard

Copied

No, the selection state of layers is not exposed to the Scripting model.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2010 Oct 29, 2010

Copy link to clipboard

Copied

But the visibility of Layers is. You could try turning off the Layers you don't want and then selecting the ones still visible.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 29, 2010 Oct 29, 2010

Copy link to clipboard

Copied

Ooh, that could work Larry.  I'm tied up with other work so I can't do too much research right now.

How would I address visible layers only?  Would it be in this initial statement?

Instead of:

var docRef = app.activeDocument;

It might be:

var docRef = app.activeDocument.visibility.true;

(im sure that syntax is not correct for visibility --  any advice on that attribute would be much appreciated)

thanks in advance!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2010 Oct 29, 2010

Copy link to clipboard

Copied

You would probably need to reference the layers property of the document.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2010 Oct 29, 2010

Copy link to clipboard

Copied

Kevin,

Your guess would only test if the active document is visible But at least you got the right property name!

What Larry says; you need to test "visible" per layer. I don't have the opportunity to test it right now, but here is the last script from this thread, with a "visible" test blatantly injected where it seemed appropriate:

(I see a couple of odd programming 'choices' in this script but it might be because it evolved throughout the discussion -- a total rewrite would oblige me to test it before posting ...)

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

    var myNumber = 1;

    for (var i = layers.length-1; i >= 0; i--) {

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

         {

          var x = layers.length.toString().length;

          var z = zeroPad(myNumber);

          if (layers.name != 'ALWAYS') { 

               if (layers.name != 'GRID') {  

                    layers.name = z + '-' + name;

                    myNumber = myNumber + 1;

                    continue;

               }

          }

        }

    }

    // myNumber = 1;

}

function zeroPad(num) {

    var tmp = num.toString();

    while (tmp.length < 2) {tmp = '0' + tmp;}

    return tmp;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 02, 2010 Nov 02, 2010

Copy link to clipboard

Copied

Jongware, that is totally doing what I want it to do.


Thanks so much to you, Larry and everybody else !!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines