Skip to main content
Participant
February 13, 2008
Question

PS3 script to batch rename layers

  • February 13, 2008
  • 11 replies
  • 14785 views
Sometimes, I make copies of layers (for example: copy little diamonds to align into a shape of the letter M). Each time I "alt+drag" the content in that layer (to make a copy of it), PS makes a copy and then appends the name of the layer to something like "diamond copy 3". After making a ton of copies (and because I'm anal about having actual names for each layer), I want each layer to read "diamond". Instead of manually renaming all these layers, is there a way to highlight the affected layers and choose to rename all layers according to what you want?
This topic has been closed for replies.

11 replies

Inspiring
October 11, 2011

This is very handy, thanks for creating it.

Suggestion: It would be very helpful to be able to rename all your selected files in one shot, ie change 25 layers all named "shape 2" to "button".

Cheers,

J

MarkWalsh
Inspiring
March 13, 2008
I made a quick rewrite to the renameLayer function. This should fix the problem with invisible layers. I ran the script on a large document just now, and it did take a while (although MUCH faster than would be manually, plus more accurate). It switches to the layer to rename it, and switches back to the original selected layer afterwards. I will rewrite it later to switch to the layer before renaming (hopefully that should solve that slowdown).

function renameLayer(objectRef) {
var theRegEx = new RegExp(/(\s*copy\s*\d*)+$/)

if (theRegEx.test(objectRef.name)) {
// save state of layer (visible or invisible)
var layerVisible = objectRef.visible
var indexNumber = 0
indexnumber = objectRef.name.indexOf(" copy")
objectRef.name = objectRef.name.substr(0,indexnumber)
objectRef.visible = layerVisible
}
return 0
}
May 13, 2010

Hi Mark,


thanks for this, worked a treat and saved me a whole lot of 'fun' in removing copy from each layer.

Participant
July 13, 2010

Hello Mark,

Also thanks for sharing the script, very usefull. I'm trying to add something to the script so that only selected layers in the document where layers are selected are processed with your renameLayer function. But i can't seem to find the correct code to only get the selected layers and layers sets

from this document. Is this even possible to do ? I'm not an experienced coder myself but I can read your script (and write fairly basic scripts). I was trying to fill an array with the selected layer and layer-sets that needed to be renamed, but I even got stuck there.

Thanks,

Fred

Known Participant
March 13, 2008
I must say that in my scripts, i had quite a few problems to get around while working with layers! (and not all where solved!)
MarkWalsh
Inspiring
March 13, 2008
It looks like changing the name of the layer makes it visible (unless it is the selected layer). I'll see if I can find the time to edit the script to activate each layer before it renames it. This should solve that problem. I'd also like it to restore the state of the layer sets that it opens to select each layer, and if I have it select each layer before renaming, then it should also end with the originally selected layer selected.
Known Participant
March 12, 2008
I have tested it.. it works like advertised (including layers in groups).

The script is fast on small sample but get quite slow on heavy documents ... so maybe my problem is more related to document size and maybe not worth investigating other scripting alternatives (but i'm pretty sure that on the same set your script would be faster).

The only bug i have found is that the script dont keep track of the visibility of layers. If a layer is invisible (when you do the renaming), the layer should be invisible when you are done with the processing.

That "problem" is also visible in a normal user interaction through the interface (select a layer make the layer visible). It's questionable in the user interface and a real problem with automation even if you record and set back the property. It must be clear to the users that if a script is crashing that document need to be reloaded!
MarkWalsh
Inspiring
March 12, 2008
Yes, it will handle all layers in every layer set. (in every opened file)

If you have a lot of layers, it could take a while as it opens each layer set and scrolls through every layer. On the files I've used it on, it runs pretty speedy. I think it will also run much faster than an applescript.
Known Participant
March 12, 2008
Hi!

I will surely have a look at the script... Does it handle well hierarchical nested layers?

In my current project, i have some documents that are very heavy (by exemple more than 1000 layers!).

The automated renaming of around 300 layers in particular document is a process that take as much as 7 hours! (Applescript on a 1.8 G5 computer/4go ram).

It's not fully clear to me if it's the logic of the script/the scripting api speed or the heavy use of PSD document that is at fault..

If only i had more time for pluggin developpement! :)

Eric
Participant
March 1, 2008
Thanks... works like a charm. This will save me a lot of time.
MarkWalsh
Inspiring
February 29, 2008
Paste into a text program (notepad on PC, TextEdit on mac) and save into the 'Scripts' directory in the 'Presets' directory of your Photoshop application folder with a '.js' or '.jsx' extension (e.g. "RenameLayers.jsx").

To activate, select "Scripts" from the"File" menu and select the saved script.
Participant
February 29, 2008
Thanks for sharing... now, what do I do with this information?