Copy link to clipboard
Copied
Hello,
I need to get pageItems from a specific InDesign layer with javascript. The problem is, the layer name isn't always the same case. For example, sometimes it's "artwork", or it can be "Artwork", or it can be anything in-between.
I found this works if it's all capped:
app.activeDocument.layers.item('ARTWORK').pageItems;
I was hoping there would be a way like so, but it doesn't work:
app.activeDocument.layers.item(/ARTWORK/i).pageItems;
Is the only way to loop through all the layers beforehand to find the correct character case?
Thanks.
Hi,
layersstring = app.activeDocument.layers.everyItem().name.join('');
getIndex = layersstring.toUpperCase().indexOf('ARTWORK');
app.activeDocument.layers.itemByName(layersstring.slice(getIndex, getIndex+7)).pageItems;
should also work.
Guess my choice would be a 'normal' loop ....
Hope it'll work
Hans Gerd Claßen
Copy link to clipboard
Copied
Hi,
layersstring = app.activeDocument.layers.everyItem().name.join('');
getIndex = layersstring.toUpperCase().indexOf('ARTWORK');
app.activeDocument.layers.itemByName(layersstring.slice(getIndex, getIndex+7)).pageItems;
should also work.
Guess my choice would be a 'normal' loop ....
Hope it'll work
Hans Gerd Claßen
Copy link to clipboard
Copied
Hi Hans,
Thank you very much for your reply.
That's very clever, and definitely works. Thanks for sharing. 🙂
I agree too - I'm thinking the loop would be best. I ended up writing this:
---------------------------------------------
$.writeln(findLayer(app.activeDocument.layers, 'ARTWORK').name);
function findLayer(layerArray, capName) {
for (var i = 0; i < layerArray.length; i++) {
var iLayer = layerArray;
if (iLayer.name.toUpperCase() === capName) {
return iLayer;
}
}
throw ('"' + capName + '" layer not found!');
}
---------------------------------------------
Thanks!