How to get full layers length in the document?

I’m trying to get the whole list of layers with
```
app.activeDocument.layers.length
```
however, this only works with pixel layers, it doesn’t count groups or adjustment layers, so how can I get the full length of all the layers counting groups and adjustment layers? Before with app.activeDocument.layers you would get the whole list including any type of layer but with it now you get only pixel layers, I’m I mistaken?

Thanks!

1 Like

I remember when I was helping with DOM, the “Layers” was proxy object wrapping the array with instance of “Layer” inside. And the proxy was forwarding built-in array methods to its internal array. But all those methods can be optionally modified during that forwarding to behave in non-native way. Also, there were some extra methods. But I don’t see reason why it would do anything special with those built-in. Proxy and Reflect

DOM works as layer tree it gets layers at the level you are currently in. So in this case number of top level layers but not its children.

In action manager code layer set, frame tool and artboards have always two layers. One opening layer and one closing layer. But DOM has always only one.

1 Like

The “json” property for the document lists all the layers, I think. You could parse it and then iterate through and count the layers.

1 Like

You’re right — app.activeDocument.layers only returns top-level pixel layers now. To get all layers including groups and adjustment layers, you’ll need to loop through layerSets recursively. Something like:

function getAllLayers(doc) {
let layers = ;
for (let i = 0; i < doc.layers.length; i++) {
let layer = doc.layers[i];
layers.push(layer);
if (layer.typename === “LayerSet”) layers = layers.concat(getAllLayers(layer));
}
return layers;
}

Hope this will help you.

2 Likes

Thank you! The recursive function worked! Had something like this in another plugin but I thought it might be an easier way, thanks for sharing!!

Instead of this, I’d check against a constant LayerKind.GROUP

LayerKind.GROUP refers directly to Photoshop internal layer kinds. While layer.typename === "LayerSet" refers to the class name in UXP DOM. LayerSet is better if you need to retrieve types for TypeScript or know the class of the object. While LayerKind is info about what is in PS. However, both should currently work fine in this case.

But in this specific condition case, isn’t it better to rely on the Ps internals, than on a class name/type, where typescript isn’t really even involved? :thinking:

It depends on how Arboards or FrameTool could possibly be implemented in the future. Right now it is same in PS and UXP. There is no LayerKind.ARTBOARD

1 Like

This was my solution

``
async function getAllLayers(obj) {
const outLayers = [];
const layers = obj.layers;
for (let i = 0; i < layers.length; i++) {
const layer = layers[i];
outLayers.push(layer);
if (layer.kind === constants.LayerKind.GROUP) {
outLayers.push(…await getAllLayers(layer));
}
}
return outLayers;
}
``

1 Like