Layer Sets (groups)

Engaged ,
Dec 07, 2011 Dec 07, 2011

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

4.8K

Likes

Translate

Translate

Report

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

correct answers 1 Correct Answer

Adobe Community Professional , Nov 17, 2014 Nov 17, 2014

Likes

Translate

Translate
Adobe Community Professional ,
Dec 07, 2011 Dec 07, 2011

Copy link to clipboard

Copied

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

          };

Likes

Translate

Translate

Report

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
Participant ,
Nov 16, 2014 Nov 16, 2014

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Nov 16, 2014 Nov 16, 2014

Copy link to clipboard

Copied

How are the Layers you are looking for defined?

Anyway, Action Manager code would likely considerably faster.

Likes

Translate

Translate

Report

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
Participant ,
Nov 17, 2014 Nov 17, 2014

Copy link to clipboard

Copied

They are defined with unique names in upper case.

How would the action manager work with the script?

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Nov 17, 2014 Nov 17, 2014

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

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
Participant ,
Nov 17, 2014 Nov 17, 2014

Copy link to clipboard

Copied

Thanks, this is a great script. I tested the script with PSCC 2014 on two separate files and noticed that it is not able to recognize the top most layer in the layer pallet.

If I move the top layer down in the layer pallet then the scripts finds the layer. Otherwise, the script finds any layer below the top layer even in a layer set.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 17, 2021 Apr 17, 2021

Copy link to clipboard

Copied

LATEST

Likes

Translate

Translate

Report

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
Explorer ,
Jun 07, 2017 Jun 07, 2017

Copy link to clipboard

Copied

Hi

I am using your layer extraction. It works great. I can print out all text elements. I am trying to remove line breaks within the text.

Here is what I have, but it does not remove them. Am I calling the layers correctly?

for (n=0; n<theLayers.length; n++){

    if (theLayers.kind == "LayerKind.TEXT") {  

         theLayers.textItem.contents = theLayers.textItem.contents.replace(/-\r/g," "); 

    }

}

Thanks

Beate

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jun 08, 2017 Jun 08, 2017

Copy link to clipboard

Copied

Would it not be

/\r/

instead of

/-\r/

Likes

Translate

Translate

Report

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