A Script to Find and Replace Layer Names

Participant ,
Mar 19, 2013 Mar 19, 2013

Copy link to clipboard

Copied

Are there any scripts to find and replace layer names?

There is an excellent script available for Photoshop which allows you to not only replace words in layer names, but also insert words as Prefixes, Suffixes and Sequential Numbers.

The illustrator version of this script only allows sequential numbering: It doesn't offer find and replacing of words.

Ideally, it would be great if there was something that could do multiple find and replaces in one go:

(e.g.

You have layers like this Car, Dog, Bat

You enter: car(Option1), dog(Option2), Bat(Option3)

Your layers then become: Option1, Option2, Option3).

)

TOPICS
Scripting

Views

29.0K

Likes

translate

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

Most Valuable Participant , Oct 15, 2013 Oct 15, 2013
big_smile, that's a very good start! Step 1 of Learning How To Script is indeed, adjusting an existing simple script to make it do more complicated things. (And usually then "break something", which is also a required part of the process.)You are correct in your observation this is repetitive stuff. For one or two different items that wouldn't be a problem, but in longer lists you soon get lost.The usual way of working with find-change lists is to build an array:var layernames = [[ 'FHairBowlBoy...

Likes

translate

Translate

Translate
Most Valuable Participant ,
Mar 19, 2013 Mar 19, 2013

Copy link to clipboard

Copied

Loop through all layers and rename like this example (renames only the toplevel layer):

var aDoc = app.activeDocument;

var option1 = prompt ("Give a new name", aDoc.layers[0].name, "new layer name");

aDoc.layers[0].name = option1;


A dialog box will be however better to display all layer names.

Have fun.


Likes

translate

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
Participant ,
Mar 19, 2013 Mar 19, 2013

Copy link to clipboard

Copied

Thanks!

Is there any way I can modify the script to rename multiple layers in one go.

E.g.

I can tell the script that I want all instances of Cat to become option 1, All instances of Dog to become option 2) etc.

Thanks!

Likes

translate

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
Most Valuable Participant ,
Mar 19, 2013 Mar 19, 2013

Copy link to clipboard

Copied

All instances???

Likes

translate

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 19, 2013 Mar 19, 2013

Copy link to clipboard

Copied

You just need a loop to recurse all layers of layers… Yes it can be done…

Likes

translate

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
Participant ,
Apr 29, 2013 Apr 29, 2013

Copy link to clipboard

Copied

I have been trying to do this. Here is my script so far:

var doc = app.activeDocument;

// loop through all layers

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

//Set up variables for current and new name

          var currentName = "car";

          var newName = "option8";

//Set up Variable to access layer name

          var currentLayer = app.activeDocument.layers;

          if (currentLayer.name == currentName) {

                    currentLayer.name = newName;

          }

}

It works, but I can only find and replace one set at a time. Is it possible to list all the sets of find and replacements I would like to make?

(e.g. so the script tell illustor for 'car' to become 'option1', 'banana' to becomes 'optionB', 'frog' to become 'jumping animal'

Thanks for any help that can be offered.

Likes

translate

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
Adobe Community Professional ,
Apr 29, 2013 Apr 29, 2013

Copy link to clipboard

Copied

Hi big_smile, I made an Illustrator version of that photoshop script, see if it helps

http://forums.adobe.com/message/4779181#4779181

Likes

translate

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
Participant ,
Oct 15, 2013 Oct 15, 2013

Copy link to clipboard

Copied

Sorry to bump up this thread, but I have a question which relates to the opening post, so I thought it was better than starting a new thread.

This is the script I am using to rename my layers:

// JavaScript Document

var doc = app.activeDocument;

// loop through all layers

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

          //Set up variables for current and new name

          var currentName = "FHairBowlBoy *Hair";

          var newName = "Hairboy1";

          //Set up Variable to access layer name

          var currentLayer = app.activeDocument.layers;

          if (currentLayer.name == currentName) {

                    currentLayer.name = newName;

          }

          //Set up variables for current and new name

          var currentName = "FHairCurlyafroBoy *Hair";

          var newName = "Hairboy2";

          //Set up Variable to access layer name

          var currentLayer = app.activeDocument.layers;

          if (currentLayer.name == currentName) {

                    currentLayer.name = newName;

          }

          //Set up variables for current and new name

          var currentName = "FHairSpikyBoy *Hair";

          var newName = "Hairboy3";

          //Set up Variable to access layer name

          var currentLayer = app.activeDocument.layers;

          if (currentLayer.name == currentName) {

                    currentLayer.name = newName;

          }

}

It works great, but it is very repetitive which makes it difficult to maintain when I want to change values. Is there anyway I can condense the code to make it easier to maintain.

(PS, I know CarlosCanto posted an excellent script in post 6, but the Illustrator version doesn't work for my purposes).

Likes

translate

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
Most Valuable Participant ,
Oct 15, 2013 Oct 15, 2013

Copy link to clipboard

Copied

big_smile, that's a very good start! Step 1 of Learning How To Script is indeed, adjusting an existing simple script to make it do more complicated things. (And usually then "break something", which is also a required part of the process.)

You are correct in your observation this is repetitive stuff. For one or two different items that wouldn't be a problem, but in longer lists you soon get lost.

The usual way of working with find-change lists is to build an array:

var layernames = [

[ 'FHairBowlBoy *Hair', 'Hairboy1' ],

[ 'FHairCurlyafroBoy *Hair', 'Hairboy2' ],

[ 'FHairSpikyBoy *Hair', 'Hairboy3' ],

];

The general idea is to loop over all names, check if the current layer name is "layernames[0]" (the left column) and if so, rename it to "layernames[1]" (the right column). If you know how to write a loop in Javascript, then you can implement this right away.

However ..

A more advanced way to do this doesn't even need loop to over all layernames -- instead you can immediately "get" the correct name per layer! It's magic! Almost!

The trick is to use a Javascript object instead of an array. Javascript objects are nothing special; Illustrator's 'layers' is an array of objects, and each object "layer" has a property "name", whose value you can read and set. What I do here is create a new object, where the "name" part is the original layer name and its value is the new layer name. All you need to check for per each layer is if there is a property 'object.originalLayerName', and if so, assign its value to that layer name.

This looks a bit like the array above, except that (1) you use {..} instead of [..] to create an object, and (2) you add "name:value" pairs instead of "value" only (actually, the 'name' of a value in an array is simply its number).

So this is what it looks like:

// JavaScript Document
var doc = app.activeDocument;

// name indexed object
var layernames = {
'FHairBowlBoy *Hair':'Hairboy1',
'FHairCurlyafroBoy *Hair':'Hairboy2',
'FHairSpikyBoy *Hair':'Hairboy3'
};

// loop through all layers
for (var i = 0; i < doc.layers.length; i++)
{
//Set up Variable to access layer name
var currentLayer = app.activeDocument.layers;

if (layernames[currentLayer.name])
{
  currentLayer.name = layernames[currentLayer.name];
}
}

Enjoy!

Likes

translate

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
Participant ,
Oct 16, 2013 Oct 16, 2013

Copy link to clipboard

Copied

Thanks, that's awsome!

Is there any way I can do a partial find and search.

E.g. Change all instances of HairBoy to CatBoy while still keeping the original number intact.

Thanks, again.

Likes

translate

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 ,
Jan 13, 2014 Jan 13, 2014

Copy link to clipboard

Copied

Hi all,

What happens when I need to rename only one word of the layer's name?

I have several cartoon characters, each of them is made of elements like arms, legs etc. Something like 100 layers per file.

Every layer has a name like this:

15_JOHN_F_ARM_DX

(layer no. 15, John character, forward, right arm).

I need to change it into this:

16_JOHN_F_ARM_SX

or:

15_MARY_F_ARM_DX

Your beautiful script above allows only exact renaming. Is there any possibility to make a search like:

search *JOHN* and change it to *MARY*?

thanks a lot!

Likes

translate

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
Participant ,
Jan 16, 2014 Jan 16, 2014

Copy link to clipboard

Copied

@Zipbong Try this. It is based on this script by Muppet Mark.

function renameText() {

     if (app.documents.length == 0) return;

  var docRef = app.activeDocument;

     recurseLayers(docRef.layers);

}

    

renameText();

function recurseLayers(objArray) {

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

          objArray.name = objArray.name.replace(/\s*OLDTEXT\s*\d*/, 'NEWTEXT');         

          if (objArray.layers.length > 0) recurseLayers(objArray.layers);

     }

}

Likes

translate

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 ,
Jan 18, 2014 Jan 18, 2014

Copy link to clipboard

Copied

A big thank you to big_smile! and to Muppet Mark as well. It is exactly what I need. It works.

have a nice day!

Lorenzo

big_smile wrote:

@Zipbong Try this. It is based on this script by Muppet Mark.

function renameText() {

     if (app.documents.length == 0) return;

  var docRef = app.activeDocument;

     recurseLayers(docRef.layers);

}

    

renameText();

function recurseLayers(objArray) {

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

          objArray.name = objArray.name.replace(/\s*OLDTEXT\s*\d*/, 'NEWTEXT');         

          if (objArray.layers.length > 0) recurseLayers(objArray.layers);

     }

}

Likes

translate

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 ,
Jun 05, 2015 Jun 05, 2015

Copy link to clipboard

Copied

This script breaks for me after renaming 1 layer.

It says there's an issue with line 12. It's for photoshop CC. Any ideas on how to get it working?

Likes

translate

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
Enthusiast ,
Jan 21, 2020 Jan 21, 2020

Copy link to clipboard

Copied

Likes

translate

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