Skip to main content
yanghansyuan
Participant
July 18, 2018
Answered

How to find empty group/folder with javaScript?

  • July 18, 2018
  • 1 reply
  • 2440 views

Hi,

I'm writing a script to do some process with several custom layer groups. There might have some empty groups that I need to ignore in my case.

So I'd like to use the script to find empty groups, how can I achieve that?

What I'm thinking is to get every layerSets and check it's length. If it's equal to 0 then is empty.  But "length" of layerSets seems not work for this.

So which property should I use? Or any better way for this?

Thank you

This topic has been closed for replies.
Correct answer frmorel

This works for me (Photoshop CS6 on Mac):

if (app.documents.length > 0) { // check if file is opened

    var thisDoc = app.activeDocument;

    var strResult = "Document layers' list:";

    var tempLength = 0;

    for (var i = 0 ; i < thisDoc.layers.length ; i++) { // loop through single layers or groups

        var activeLayer = thisDoc.layers;

        try{

            tempLength = "contains " + activeLayer.layers.length + " layers";

        }catch (err){

            tempLength = "(not a group)";

        }

        strResult += "\n[" + i + "] " + thisDoc.layers.name + ": " + tempLength;

    }

    alert(strResult);

}

1 reply

frmorelCorrect answer
Inspiring
July 18, 2018

This works for me (Photoshop CS6 on Mac):

if (app.documents.length > 0) { // check if file is opened

    var thisDoc = app.activeDocument;

    var strResult = "Document layers' list:";

    var tempLength = 0;

    for (var i = 0 ; i < thisDoc.layers.length ; i++) { // loop through single layers or groups

        var activeLayer = thisDoc.layers;

        try{

            tempLength = "contains " + activeLayer.layers.length + " layers";

        }catch (err){

            tempLength = "(not a group)";

        }

        strResult += "\n[" + i + "] " + thisDoc.layers.name + ": " + tempLength;

    }

    alert(strResult);

}

yanghansyuan
Participant
July 19, 2018

cool, work like a charm. Thanks

Could I  ask what the difference in between layers, layerSet and layerSets?  I originally thought layerSets is mean "group". But your code is actually used "layers.length".

I read the document not still not sure the difference.

Thank you

Inspiring
July 19, 2018

Sure, you can ask... But I can't give you an answer .

I'm very far to be a Photoshop scripting master and I only used part of sample scripts found in Adobe documentation. I'm using an extensive "try/error" method.