Javascript get all Layers inside a Layer Group tree
Copy link to clipboard
Copied
Hi there,
How can I get all the Layers inside a group tree from an extension?
I have the following tree:
Group1
> Layer 1
> Layer 2
> Group 2
> > Layer 3
> > Layer 4
> > Group 3
> > > Layer 5
> > > Layer 6
> Layer 3
So in this case I should get this: Layer 1, Layer 2, Layer 3, Layer 4, Layer 5, Layer 6
Thanks in advance
Copy link to clipboard
Copied
You need to use a recursive function then you do whatever you want with all the layers in the array layerList:
var doc = activeDocument;
var layerList = new Array()
getLayers(doc);
function getLayers(lSet){
for(var i=0;i<lSet.layers.length;i++){
doc.activeLayer = lSet.layers;
if(doc.activeLayer.typename == 'LayerSet'){
if(doc.activeLayer.layers.length>0){
getLayers (doc.activeLayer)
}
}
else{
layerList.push(doc.activeLayer)
}
};//end loop
};//end function

