Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript get all Layers inside a Layer Group tree

Participant ,
Sep 11, 2015 Sep 11, 2015

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

3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2015 Sep 15, 2015
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines