Skip navigation
pavelfuksa
Currently Being Moderated

Script: rename layers?

Feb 15, 2010 11:46 PM

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

 
Replies
  • Currently Being Moderated
    Feb 16, 2010 4:22 AM   in reply to pavelfuksa

    This is straight top level layers only…

     

    #target illustrator

     

    var docRef = app.activeDocument;

     

    with (docRef) {

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

    layers[i].name = i + 1;

    }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 16, 2010 11:16 AM   in reply to pavelfuksa

    #target illustrator

     

    var docRef = app.activeDocument;

     

    with (docRef) {

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

              layers[i].name = layers.length - i;

         }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 16, 2010 11:25 AM   in reply to pavelfuksa

    happy to have found this — thanks!!

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 3, 2010 11:07 PM   in reply to Muppet Mark

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 4, 2010 3:58 AM   in reply to steven1978leon

    #target illustrator

     

    var docRef = app.activeDocument;

     

    with (docRef) {

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

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

    }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 4, 2010 7:17 AM   in reply to Muppet Mark

    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-illustra tor/

     

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 4, 2010 9:42 AM   in reply to halfshark

    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[i].name != 'ALWAYS') {

    layers[i].name = myNumber + 'n ' + layers[i].name;

    myNumber = myNumber + 1;

    continue;

    }

    }

    myNumber = 1;

    }

     

    Should number from bottom up skipping layers names 'ALWAYS'

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 4, 2010 2:16 PM   in reply to Muppet Mark

    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[i].name != 'ALWAYS') {    
                    layers[i].name = myNumber + '-' + activeDocument.name;
                    myNumber = myNumber + 1;
                    continue;
            } 
        }
        myNumber = 1;
    }

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 4, 2010 3:07 PM   in reply to Muppet Mark

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 5, 2010 3:24 AM   in reply to halfshark

    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[i].name != 'ALWAYS') {    

    layers[i].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;

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 5, 2010 2:45 PM   in reply to Muppet Mark

    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[i].name != 'ALWAYS') { 
                if (layers[i].name != 'GRID') {  
                    layers[i].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;
    }
           
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 6, 2010 1:22 AM   in reply to halfshark

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 6, 2010 1:23 AM   in reply to halfshark

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 28, 2010 2:19 PM   in reply to Muppet Mark

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 29, 2010 2:40 AM   in reply to kevinpales@hotmail.com

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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 29, 2010 9:24 AM   in reply to [Jongware]

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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 29, 2010 9:41 AM   in reply to Larry G. Schneider

    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!

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 29, 2010 11:09 AM   in reply to kevinpales@hotmail.com

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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 29, 2010 12:55 PM   in reply to kevinpales@hotmail.com

    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[i].visible == true// <--- Testing .. 1,2,3.
             {
              var x = layers.length.toString().length;
              var z = zeroPad(myNumber);
              if (layers[i].name != 'ALWAYS') {  
                   if (layers[i].name != 'GRID') {   
                        layers[i].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;
    }
     
    

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 2, 2010 8:15 AM   in reply to [Jongware]

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


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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 4:53 PM   in reply to pavelfuksa

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 5:37 PM   in reply to Navarro Parker

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

     

    Screen shot 2012-07-05 at 5.34.38 PM.png

     

    Screen shot 2012-07-05 at 5.34.52 PM.png

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points