skip to Main Content

In the Layers panel, Photoshop has a dropdown option to ‘Collapse All Groups’. Is there a reverse option to expand one or all groups or a way to do it in Javascript?

3

Answers


  1. Any hoops, to collapse all groups just use Scriptlistner code:

    function collapse_all_groups()
    {
      // =======================================================
      var idcollapseAllGroupsEvent = stringIDToTypeID( "collapseAllGroupsEvent" );
      var desc46 = new ActionDescriptor();
      executeAction( idcollapseAllGroupsEvent, desc46, DialogModes.NO );
    }
    

    And there doesn’t appear to be a "expandAllGroupsEvent"

    However, I eventually found the answer here

    function openAllLayerSets(parent)
    {
        for (var setIndex = 0; setIndex < parent.layerSets.length; setIndex++) 
        {
            try
            {
              app.activeDocument.activeLayer = parent.layerSets[setIndex].layers[0];
            }
            catch(e)
            {
              alert(e + app.activeDocument.activeLayer);
            }
            openAllLayerSets(parent.layerSets[setIndex]);
        }
    }
    
    openAllLayerSets(app.activeDocument);
    
    Login or Signup to reply.
  2. Here you go, answer is derived from

    https://community.adobe.com/t5/photoshop/expand-collapse-a-group-via-javascript/td-p/7286289/page/2?page=1%3F

    var groupname = app.activeDocument.activeLayer.name // name of active layer 
    var idungroupLayersEvent = stringIDToTypeID( "ungroupLayersEvent" ); // ungroup layer event 
    
    var desc14 = new ActionDescriptor();
    
    var idnull = charIDToTypeID( "null" );
    
    var ref13 = new ActionReference();
    
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref13.putEnumerated( idLyr, idOrdn, idTrgt );
    desc14.putReference( idnull, ref13 );
    
    executeAction( idungroupLayersEvent, desc14, DialogModes.NO );
    
    var idMk = charIDToTypeID( "Mk  " ); // make action , note the space
    var desc15 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    
    var ref14 = new ActionReference();
    
    var idlayerSection = stringIDToTypeID( "layerSection" );
    ref14.putClass( idlayerSection );
    desc15.putReference( idnull, ref14 );
    var idFrom = charIDToTypeID( "From" );
    
    var ref15 = new ActionReference();
    
    var idLyr = charIDToTypeID( "Lyr " ); // <-- note the space
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref15.putEnumerated( idLyr, idOrdn, idTrgt );
    desc15.putReference( idFrom, ref15 );
    
    executeAction( idMk, desc15, DialogModes.NO );
    app.activeDocument.activeLayer.name = groupname // recall group name
    
    Login or Signup to reply.
  3. this works in UXP JavaScript land in Ps 23.5.0
    showing both getting the expanded state and switching/or toggling it; to expand all currently I’d simply walk the layers properties of group layers (layers where layer.kind === 'group') from the desired starting point (or photoshop.app.activeDocument.layers) as I don’t know if there are more options applicable to this approach

    function toggleGroupLayer(_id=-1){
        const photoshop = require('photoshop');
        photoshop.action.batchPlay([
    
            { _obj: "multiGet", _target: {_ref: "layer", _id}, extendedReference: [[
               "layerSectionExpanded"
            ]] }
        ], {}).then(([{layerSectionExpanded}])=>{
    
        console.log(`before toggling:`,{_id, layerSectionExpanded});
        layerSectionExpanded = !layerSectionExpanded;
        return photoshop.core.executeAsModal(function _executeAsModal(context, descriptor){
            return photoshop.action.batchPlay([{
                _obj: "set",
                _target: [{ _ref: "property", _property: "layerSectionExpanded" }, { _ref: "layer", _id }],
                to: layerSectionExpanded
            }], {}).then(res=>{
                return descriptor;
            }).catch(console.error);
        },
        { commandName: 'toggle group layer expanded', descriptor: {layerSectionExpanded, _id} }
        );
    
        }).then(console.log).catch(console.error);
    
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search