Action or Script to Expand ( unfold ) Layer Groups. ?

New Here ,
Jul 18, 2022 Jul 18, 2022

Copy link to clipboard

Copied

Bare with me, I know nothing useful about scripting.
I am building an action that contains several steps, one of the steps involves duplicating a layer group and then selecting a layer inside that layer group. I cannot find any way to open ( unfold ) a layer group. Selecting, 'next' or 'previous' only selects the top layers and does not open the group. Any suggestions would be greatly appreciated.

 

🙂

 

TOPICS
Actions and scripting

Views

103

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 , Jul 18, 2022 Jul 18, 2022
var g = (ad = activeDocument).activeLayer; if (g.typename == 'LayerSet' && g.layers.length) ad.activeLayer = g.layers[0]; ad.activeLayer = g

Likes

Translate

Translate
Adobe Community Professional ,
Jul 18, 2022 Jul 18, 2022

Copy link to clipboard

Copied

var g = (ad = activeDocument).activeLayer; if (g.typename == 'LayerSet' && g.layers.length) ad.activeLayer = g.layers[0]; ad.activeLayer = g

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 ,
Jul 18, 2022 Jul 18, 2022

Copy link to clipboard

Copied

@jazz-y – Great work as always!

 

This got me thinking, there is a native command to collapse all groups, however, there isn't one to collapse just the selected set. Can you post code for that, please?

 

Edit: I got that backwards... Additionally, as there is a collapse all groups command – what about a script to expand all groups?

 

 

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 ,
Jul 18, 2022 Jul 18, 2022

Copy link to clipboard

Copied

There is a "layerSectionExpanded" key in PIStringTerminology.h. It probably describes the state of a particular group. I tried to use it in different ways, but did not succeed. As a workaround, we can save the group properties (name, blending mode, effects, etc.) as completely as possible, ungroup it and create again.

 

collapseAllGroupsEvent:

 

executeAction(stringIDToTypeID('collapseAllGroupsEvent'), new ActionDescriptor(), DialogModes.NO)

 

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 ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

Sometimes I think that I have forgotten more things than I remember! Looks like the code could still use a little more work...

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-quot-collapse-all-groups-l...

 

https://gist.github.com/MarshySwamp/a39e3e1efee41174b3b92ee68c40fcd8

 

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 ,
Jul 23, 2022 Jul 23, 2022

Copy link to clipboard

Copied

LATEST

 

OK, so this rather verbose function will collapse only the active layer group:

 

collapseActiveGroup();

function collapseActiveGroup() {
    // Stephen Marsh, 23rd July 2022, v1.0
    if (app.activeDocument.activeLayer.typename === "LayerSet") {
        // Doc name variable
        var origDoc = app.activeDocument.name;
        // Create the temp doc from active layer group
        newDocFromLayer("_tempDoc");
        // Collapse the group
        executeAction(stringIDToTypeID('collapseAllGroupsEvent'), new ActionDescriptor(), DialogModes.NO);
        // Dupe the layer back to the original doc
        dupeLayer();
        // Close the temp doc
        activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        // Select backward layer
        nextLayer('down');
        // Delete the original expanded group
        activeDocument.activeLayer.remove();
        // Select the new collapsed group
        nextLayer('up');
    } else {
        //alert("Not a layer group!");
    }

    // Functions
    function newDocFromLayer(docName) {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        var reference2 = new ActionReference();
        reference.putClass(s2t("document"));
        descriptor.putReference(s2t("null"), reference);
        descriptor.putString(s2t("name"), docName);
        reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(s2t("using"), reference2);
        descriptor.putInteger(s2t("version"), 0);
        executeAction(s2t("make"), descriptor, DialogModes.NO);
    }

    function dupeLayer() {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var list = new ActionList();
        var reference = new ActionReference();
        var reference2 = new ActionReference();
        reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(s2t("null"), reference);
        reference2.putName(s2t("document"), origDoc);
        descriptor.putReference(s2t("to"), reference2);
        descriptor.putInteger(s2t("version"), 5);
        list.putInteger(0);
        descriptor.putList(s2t("ID"), list);
        executeAction(s2t("duplicate"), descriptor, DialogModes.NO);
    }

    function nextLayer(direction) {
        // https://gist.github.com/joonaspaakko/048c9b58ccbb6e6f44c894bf4ce30b68
        // direction (↑): "up" or "above"
        // direction (↓): "down" or "below"
        var doc = app.activeDocument;
        var tempDoc = doc.duplicate();
        var layer1 = tempDoc.activeLayer;
  
        // Turn background layer into a normal layer
        var lastLayer = tempDoc.layers[tempDoc.layers.length - 1];
        function cTID(s) { return app.charIDToTypeID(s); }
        function sTID(s) { return app.stringIDToTypeID(s); }
        var layer1ID = activeLayerID();
        tempDoc.activeLayer = lastLayer;
        var bgLayerExists = lastLayer.isBackgroundLayer;
        if (bgLayerExists) { lastLayer.isBackgroundLayer = false; }
        try { selectLayerByID(layer1ID); } catch (e) { }
        // Select all layers
        var desc23 = new ActionDescriptor();
        var ref5 = new ActionReference();
        ref5.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
        desc23.putReference(cTID('null'), ref5);
        executeAction(sTID('selectAllLayers'), desc23, DialogModes.NO);
        // Make active layers visible
        var desc209 = new ActionDescriptor();
        var list93 = new ActionList();
        var ref129 = new ActionReference();
        ref129.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
        list93.putReference(ref129);
        desc209.putList(cTID('null'), list93);
        executeAction(cTID('Shw '), desc209, DialogModes.NO);
        // Reselect the starting layer
        if (bgLayerExists) { lastLayer.isBackgroundLayer = true; }
        selectLayerByID(layer1ID);
        // Select next layer
        try { snl(direction); } catch (e) { }
        // Store the layer
        var nextLayer = activeLayerID();
        tempDoc.close(SaveOptions.DONOTSAVECHANGES);
        // Try to select the next layer using its ID
        try {
            selectLayerByID(nextLayer);
        }
        // If it fails, well assume it did so because it was a background layer... and use another method for selecting that.
        catch (e) {
            var desc299 = new ActionDescriptor();
            var ref187 = new ActionReference();
            ref187.putName(cTID('Lyr '), "Background");
            desc299.putReference(cTID('null'), ref187);
            desc299.putBoolean(cTID('MkVs'), false);
            var list138 = new ActionList();
            list138.putInteger(1);
            desc299.putList(cTID('LyrI'), list138);
            executeAction(cTID('slct'), desc299, DialogModes.NO);
        }
  
        function snl(direction) {
    
            var select;
            if (direction == 'up' || direction == 'above') {
                select = cTID('Frwr');
            }
            else if (direction == 'down' || direction == 'below') {
                select = cTID('Bckw');
            }
    
            var desc67 = new ActionDescriptor();
            var ref41 = new ActionReference();
            ref41.putEnumerated(cTID('Lyr '), cTID('Ordn'), select);
            desc67.putReference(cTID('null'), ref41);
            desc67.putBoolean(cTID('MkVs'), false);
            var list17 = new ActionList();
            list17.putInteger(5);
            desc67.putList(cTID('LyrI'), list17);
            executeAction(cTID('slct'), desc67, DialogModes.NO);
        }
  
        function activeLayerID() {
            var ref = new ActionReference();
            ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("LyrI"));
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            return executeActionGet(ref).getInteger(stringIDToTypeID("layerID"));
        }
  
        function selectLayerByID(id, add) {
            add = (add == undefined) ? add = false : add;
            var ref = new ActionReference();
            ref.putIdentifier(cTID('Lyr '), id);
            var desc = new ActionDescriptor();
            desc.putReference(cTID('null'), ref);
            if (add) {
                desc.putEnumerated(sTID('selectionModifier'), sTID('selectionModifierType'), sTID('addToSelection'));
            }
            desc.putBoolean(cTID('MkVs'), false);
            executeAction(cTID('slct'), desc, DialogModes.NO);
        }
  
    }
}

 

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
New Here ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

Thank you all for your help. 

Please forgive the extreme newbie question, but how exactly do I create this script? I am running CC2021 but I can't seem to find any information on what application to use just to simply build the script. Apparently ExtendScript is no longer available. I am on a mac running just Catalina. 

I know how to write some basic javascript inside After Effects expressions. I am just not sure what my starting point is or what app to use to actually write 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 ,
Jul 19, 2022 Jul 19, 2022

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