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

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

37.7K

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

Community Expert , 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

...

Votes

Translate

Translate
Adobe
Community Expert ,
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.


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
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!

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

Copy link to clipboard

Copied

All instances???

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 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…

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

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 ,
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

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

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 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!

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

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 ,
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!

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

     }

}

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 ,
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);

     }

}

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
Explorer ,
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?

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
Explorer ,
Mar 19, 2024 Mar 19, 2024

Copy link to clipboard

Copied

I tried to use this code but not work. Could you help to advise? Thank you!
errỏ 1302.png

// JavaScript Document
var doc = app.activeDocument;

// name indexed object
var layernames = {
'NAME':'__B__',
'LASTNAME':'__B__',
'NUMBER':'__C__'
};

// 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];
}
}

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
Enthusiast ,
Mar 19, 2024 Mar 19, 2024

Copy link to clipboard

Copied

// JavaScript Document
var doc = app.activeDocument;

// name indexed object
var layernames = {
  'NAME': '__B__',
  'LASTNAME': '__B__',
  'NUMBER': '__C__'
};

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

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

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
Explorer ,
Mar 19, 2024 Mar 19, 2024

Copy link to clipboard

Copied

LATEST

Thank you for your reply but it's not work for object/groupNAME-NUMBER.png

 

I just found this code work for object and group. But could you help to check if we can change like this

Layer: AB CD DEFGH = _D_

Layer: AB CD 4ADF68 = _D_

Layer: AB CD 4AKLDA = _D_

Layer: AB CD 56K89A = _D_

It means we just find the first string and replace a whole layer name to new name? Here is the code

/ You can change these settings:
var settings = {
// Should the script guess the name for unnamed items like <Group>, <Path>, etc:
replaceGenerics: true,
// Should the script replace "HELLO" when findText is "Hello":
caseSensitive: false
}
// What to find:
var findText = 'AB CD *'
//
// What to replace it with:
var replaceText = '__D__'

/**
*
* Don't edit below unless you know what you're doing
*
*/

Array.prototype.forEach = function (callback) {
for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};
function get(type, parent) {
if (arguments.length == 1 || !parent) parent = app.activeDocument;
var result = [];
if (!parent[type]) return [];
for (var i = 0; i < parent[type].length; i++) {
result.push(parent[type][i]);
if (parent[type][i][type])
result = [].concat(result, get(type, parent[type][i]));
}
return result || [];
}

function findReplace(find, replace, options) {
var layers = get('layers');
var pageItems = get('pageItems');
var list = [].concat(layers, pageItems);
var findRX = new RegExp(getEscapedRXString(find), !options.caseSensitive ? 'i' : null)
list.forEach(function (item) {
if (item.name.length)
item.name = item.name.replace(findRX, replace)
else
item.name = options.replaceGenerics && findRX.test(item.typename)
? item.typename.replace(/item/i, '').replace(findRX, replace)
: item.name
})
}

function getEscapedRXString(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
}

findReplace(findText, replaceText, settings)



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

Copy link to clipboard

Copied

FOr those who want a working link 

https://www.retouchpro.com/forum/tools/software/photoshop-scripting/22315-script-to-find-replace-tex...

 

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

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