I am trying to write a script for Photoshop to find if a certain layer exists in the document and then if it does, to rename it. I could do it in Python but am quite confused by the scripting used by Photoshop. So, in Python it would look something like this:
def myfunction (psd):
for (layer in psd):
if ( layer == “layer_name”):
layer = “new_layer_name”
How can this be done in Photoshop?
2
Answers
It’s quite simple provided you don’t have layer groups – and then it get’s a bit more complicated.
So, you’ve got your psd with five or so layers in, one of them is called "spoon" by accident. This will rename it from "spoon" "my layer name".
However, if you have groups then you’ll need to loop over all the layers with something like this.
Most of Photoshop’s document object collections include the built-in method
getByName(String name)
for querying the collection by name (see full list here). The method performs a case-sensitive search, returning the first object with a matchingname
property, or throwing if none found.While in this case a simple recursive function traversing across artLayers and into layerSets is most effective, one could also (ab)use the
getByName
method to perform recursive renaming…