Skip to main content
Inspiring
December 7, 2011
Answered

Layer Sets (groups)

  • December 7, 2011
  • 1 reply
  • 5905 views

Hi fellas. It's been a while!

I tend not to use groups (layer sets) myself, but other people do. I've got several scripts that loop over all layers in a psd and then peform various tasks.

Of course they fall over if layersets are involved; so I need to change that.

To test this out I wrote a script and made a simple psd; New document (any size), background, one layer (called "layer 1") and another layer ("layer 2") which is in a layer set ("a_group")

I've got a basic script here that loops through all layers (or so I thought)

<code>

var srcDoc = app.activeDocument;

var numOfLayers = srcDoc.layers.length;

for (var i = numOfLayers -1; i >= 0  ; i--)

{    var layerName = srcDoc.layers.name;

    // check for groups

    if (srcDoc.layers.typename == "LayerSet")

    {

    alert (layerName + " is a group")

    }

    else

    {

    alert (layerName + " is not in a group")

    }

</code>

The alert calls all layer names as it goes up the stack. It however misses out layer 2 (which is in a group) So what am I missing out?

Cheers

This topic has been closed for replies.
Correct answer c.pfaffenbichler

They are defined with unique names in upper case.

How would the action manager work with the script?


Script to search layer names | Adobe Community

1 reply

c.pfaffenbichler
Community Expert
Community Expert
December 7, 2011

I think you need a function that calls itself, because Layers in LayerSets are children of the LayerSet and not the document.

var theLayers = collectLayers(app.activeDocument, []);

alert (theLayers.join("\n"));

////// function collect all layers //////

function collectLayers (theParent, allLayers) {

          if (!allLayers) {var allLayers = new Array}

          else {};

          for (var m = theParent.layers.length - 1; m >= 0;m--) {

                    var theLayer = theParent.layers;

// apply the function to layersets;

                    if (theLayer.typename == "ArtLayer") {

                              allLayers.push(theLayer)

                              }

                    else {

                              allLayers = (collectLayers(theLayer, allLayers))

// this line includes the layer groups;

                              allLayers.push(theLayer);

                              }

                    };

          return allLayers

          };

Inspiring
November 16, 2014

The script reports a list of layers and layer sets from the active document.

How can this script check for a specific layer either as part of a layer set or stand alone layer in the active document?

c.pfaffenbichler
Community Expert
Community Expert
November 17, 2014

How are the Layers you are looking for defined?

Anyway, Action Manager code would likely considerably faster.