Copy link to clipboard
Copied
Hello,
When looping over layers, what is the best way to:
1. Get the "active" layer?
2. Set a new layer as active?
Also, is there an easy way to get the index of the app.activeDocument.activeLayer when not looping over layers? In other words, can I determine the activeLayer's index without using a loop to find it?
Thanks!
Setting the activeLayer property does the setting trick:
doc.activeLayer = doc.layers[1];
And getting the index seems to work using the zOrderPosition property for getting the 1-based stacking order:
alert(doc.activeLayer.zOrderPosition); // 2
Copy link to clipboard
Copied
Setting the activeLayer property does the setting trick:
doc.activeLayer = doc.layers[1];
And getting the index seems to work using the zOrderPosition property for getting the 1-based stacking order:
alert(doc.activeLayer.zOrderPosition); // 2
Copy link to clipboard
Copied
Awesome! Thanks Silly-V‌!
I can't believe I didn't think to set the activeLayer like that. It's so obvious now that you point it out. Lol.
+1 on the zOrderPosition. I saw that in the object viewer, but I wasn't sure if that was a reliable way to get the index. Much appreciated!
Also, for future readers, one way to check for active layer when looping over layers is:
_doc = app.activeDocument;
for (i = 0, l = _doc.layers.length; i < l; i++) {
current = _doc.layers;
if (current === _doc.activeLayer) {
// The `current` layer is the `activeLayer`, so do something.
}
}
Thanks a billion Silly-V‌, I totally appreciate all of your pro help!
Have a great week!
Cheers,
M