Skip to main content
Known Participant
September 1, 2014
Answered

Rename Layers: Find and Replace style renaming

  • September 1, 2014
  • 1 reply
  • 1856 views

In Illustrator, you can use the following to find and replace layer names:

var doc = app.activeDocument; 

// name indexed object 

var layernames = { 

'Bob':'Bob Front'

}; 

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

In this script "Bob" will become "Bob Front". Is there something similar for Photoshop. Looking at the Photoshop scripting guide, this script should work as the active App document and layers methods exist in Photoshop.

This topic has been closed for replies.
Correct answer pixxxelschubser

c.pfaffenbichler wrote:

What are

var layernames = {

'Bob':'Bob Front'

};

and

if (layernames[currentLayer.name])

supposed to do? Is that even JavaScript? …

Hi c.pfaffenbichler, yes, this is pure Javascript.

Try this little example:

var layernames = {'Bob':'Bob Behind'};

alert(layernames['Bob']);

// a variant

layernames['Bob'] = 'Bob in the middle';

alert("a variant:  " + layernames['Bob']);

// another variant

layernames.Bob = 'Bob Front';

alert("another variant:  " + layernames.Bob);

@big_smile,

your posted script works with photoshop (if you have only artlayers and one or more layers named with Bob)

var doc = app.activeDocument;

// name indexed object

var layernames = {'Bob':'Bob Front'};

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

        }

    }

All you have to do is to loop trough all artlayers (and layersets), but here is Javascript a slowly variant. Use ActionManager code instead. You will find many examples here in forum.

Have fun

1 reply

c.pfaffenbichler
Community Expert
Community Expert
September 2, 2014

What are

var layernames = {

'Bob':'Bob Front'

};

and

if (layernames[currentLayer.name])

supposed to do? Is that even JavaScript?

Also you should consider that Layers that reside in a Group are filial to the Group in DOM, not the Document.

big_smileAuthor
Known Participant
September 2, 2014

Thanks for the reply.

Now that I think about it, I think I got the Illustrator code from another forum, here.

Also you should consider that Layers that reside in a Group are filial to the Group in DOM, not the Document.

I will look into this, thanks! I think I might have to study Photoshop scripts a bit more first...

c.pfaffenbichler
Community Expert
Community Expert
September 2, 2014

Interesting, I have not yet set up Objects like this, so the code looked foreign to me.

How many Groups deep will the Script have to work?