I’m learning JavaScript for use with photoshop. Im trying to select a layer in a document using a wildcard. I come from a maxscript background so using “*” as a wildcard in strings.
var srcDoc = app.activeDocument;
var a = srcDoc.artLayers.getByName("*t*"); //trying to select a layer that has t in it..
app.activeDocument.activeLayer = a;
3
Answers
Thanks Cobra_Fast
Its so close its throwing back undefined not an object in the if statement line. But like you said artLayers is acting as an array, so im a bit confused.
According to this bit of documentation,
ArtLayers.getByName()
only supports searching for exact matches.If Photoshop does not provide a searching mechanic, you will have to iterate through all elements in attempt to find matching ones.
Your code could look something like this:
Welcome to SO. Despite the downvotes I like your question, I’m surprised PS doesn’t have a fuzzy layer name search already.
I’ll get you up to speed:
Looping over layers is easy.
However, this is made more complex with layer groups and becomes as right pain at times. So you have to have a recursive function that’ll find them all. It took me a while to get my head around that one! Since we’re only dealing with art layers we’ll add them to a special artlayers array.
You can then use regular expressions (unavailable in Maxscript:)) to look over an array of layer names.