Skip to main content
Participant
June 28, 2013
Answered

script to rename layers alphabetically

  • June 28, 2013
  • 1 reply
  • 1304 views

Hi everyone,

I have a renaming script that works only on numbers and I want to make it work for letters also.

What I`m trying to achieve is to select some layers --> load the script --> enter the desired name of the layer and in brackets insert the letter from where to start. Then the script iterates through the alphabet renaming the layers and adding the coresponding letter to the name and removing the brackets. For example, I select 4 layers, load the script and when the prompt comes on... enter the name:      test . At the end I should have the layers renamed testC,testD,testE,testF

Any help would be appreciated!

Thanks

This topic has been closed for replies.
Correct answer CarlosCanto

Ok, I've managed to rename the layers based on selection but I've encountered another problem.

If I select 4 layers for example and run the script starting from letter "A"..it goes well..it renames the four layers from A to D. My problem now is that if I want to start renaming those 4 layers from letter B or C, it will skip layer 1 (if letter B) or layer 2(if letter C). So basically there should be a way to reset the counter when starting to rename and rename first letter with my specified letter and iterate.

Here is my code so far:

var doc = app.activeDocument,

sel = doc.selection,

rename = prompt("Enter the name:","");

done = 0;

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var r = /\[\w+\]/i;

var matching = rename.match( r );

var letter = str.search( matching );

if (letter>=0)

{

   for(i=letter; i<sel.length;i++)

   {

        var nextChar = str.charAt(i);

      if(nextChar){

            theRenaming=rename.replace(/\[\w+\]/, nextChar);

            sel.name = theRenaming;

            letter++;

        }

        else

        sel.name = rename;

   }   

} else

{

alert("Nothing Found!");

}

Any help would be appreciated.

I still don't know why muppet mark said that you cannot access layer names.


so, what you wanted is to rename Selected Objects, not Layers, right?

check this script, I took a different route, I added a couple of comments, if you have any trouble with it, feel free to ask

// this script Renames selected Objects with a base name plus an incremental suffix

// Carlos Canto // 07/03/2013;

// http://forums.adobe.com/thread/1243774?tstart=0

var doc = app.activeDocument;

var sel = doc.selection;

var rename = prompt("Enter the name and ONE character suffix (comma separated, no space):","test,A", "Increment Rename Selected Objects");

if (rename!=null) {// quit if pressed Cancel

    var nameArray = rename.split(",");

    var base = nameArray[0];

    var suffix = nameArray[1];

    var charCode = suffix.charCodeAt (); // convert the suffix character to Unicode

    if(sel.length>0){

        for(var z=0;z<sel.length;z++) {  

            var suffix = String.fromCharCode (charCode+z); // increase the Unicode value and convert it back to a String

            sel.name = base + suffix;

        }

    // Illustrator bug? new names don't update in the UI, this will force the layer panel to redraw

    sel[0].selected = false;

    sel[0].selected = true;

    }

    else

      alert("Nothing was selected");

}

else alert("Cancelled by User");

1 reply

Inspiring
June 28, 2013

Script has NO access to which layers you have selected in the layers palette…

MMScreenXAuthor
Participant
June 28, 2013

What about the sub-layers then?

This script does what I need but for numbers only.

var doc = app.activeDocument,

    sel = doc.selection,

    rename = prompt("Enter the name:",""),

    done = 0;

if(sel.length>0){

  var nr=rename.match(/\[\w+\]/);

  if(nr){ nr=nr[0].match(/\w+/)[0];

  }

  for(var z=0;z<sel.length;z++){   

    if(nr){

        theRenaming=rename.replace(/\[\w+\]/,nr);

        sel.name = theRenaming;

        nr++;

    } else

      sel.name = rename

  }

} else

  alert("Nothing was selected")

MMScreenXAuthor
Participant
July 2, 2013

Ok, I've managed to rename the layers based on selection but I've encountered another problem.

If I select 4 layers for example and run the script starting from letter "A"..it goes well..it renames the four layers from A to D. My problem now is that if I want to start renaming those 4 layers from letter B or C, it will skip layer 1 (if letter B) or layer 2(if letter C). So basically there should be a way to reset the counter when starting to rename and rename first letter with my specified letter and iterate.

Here is my code so far:

var doc = app.activeDocument,

sel = doc.selection,

rename = prompt("Enter the name:","");

done = 0;

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var r = /\[\w+\]/i;

var matching = rename.match( r );

var letter = str.search( matching );

if (letter>=0)

{

   for(i=letter; i<sel.length;i++)

   {

        var nextChar = str.charAt(i);

      if(nextChar){

            theRenaming=rename.replace(/\[\w+\]/, nextChar);

            sel.name = theRenaming;

            letter++;

        }

        else

        sel.name = rename;

   }   

} else

{

alert("Nothing Found!");

}

Any help would be appreciated.

I still don't know why muppet mark said that you cannot access layer names.