Skip to main content
Known Participant
March 19, 2013
Answered

A Script to Find and Replace Layer Names

  • March 19, 2013
  • 3 replies
  • 40557 views

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).

)

This topic has been closed for replies.
Correct answer Jongware

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!

3 replies

schroef
Inspiring
January 22, 2020

FOr those who want a working link 

https://www.retouchpro.com/forum/tools/software/photoshop-scripting/22315-script-to-find-replace-text-in-layer-names

 

EDIT 01-03-2023

I was contacted by someone asking about my link i posted, seems the page is down or server is not responding. It keeps looking. Luckily, it was caught by Web Archives website and they have a working backup. Guys REMEMBER this website, its very usefull and more then 50% of time, perhaps even around 80% of time. Website have been backup.

Here's is a working link

http://web.archive.org/web/20220722195913/https://www.retouchpro.com/forum/tools/software/photoshop-scripting/22315-script-to-find-replace-text-in-layer-names

big_smileAuthor
Known Participant
October 15, 2013

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).

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
October 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 *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!

big_smileAuthor
Known Participant
October 16, 2013

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.

pixxxelschubser
Community Expert
Community Expert
March 19, 2013

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.


big_smileAuthor
Known Participant
March 19, 2013

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!

pixxxelschubser
Community Expert
Community Expert
March 19, 2013

All instances???