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?
Asked
Active
Viewed 556 times
0
-
1Haha, yes it's unfortunate that people don't read the question before acting on it, while javascript based solution might work here but I've never done that level of scripting , you may try [this](https://graphicdesign.stackexchange.com/questions/5552/is-it-possible-to-collapse-expand-all-groups-in-photoshop-layers-panel) instead – Vinay Aug 12 '20 at 17:21
-
@Viney Thanks for the link and solidarity. Unfortunately that answer won't work in an action as 'Collapse All Groups will. – Jack Aug 12 '20 at 18:00
3 Answers
2
Here you go, answer is derived from
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

Vinay
- 7,442
- 6
- 25
- 48
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);

Peter O.
- 32,158
- 14
- 82
- 96

Ghoul Fool
- 6,249
- 10
- 67
- 125
-
Thanks but it doesn't work unfortunately. I get a 'Error 1302: No such element' for line 5. – Jack Aug 13 '20 at 17:50
-
Weird. Have you tried creating a small psd with a couple of layers and put those layers in a group? Then re-try the script. I've added a try /catch so you might be able to find the layer that's causing the problem – Ghoul Fool Aug 14 '20 at 07:35
0
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);
}

jimmont
- 2,304
- 1
- 27
- 29
-
Thanks! I'm not sure what you mean by 'walk the layers properties of group layers...' – Jack Sep 08 '22 at 11:55
-
group layers have child layers, as in `layer.layers = [childLayer,...]` and those can also be group layers, so walking/recursing through them to collapse/expand all isn't accounted for here in the example; if this answer works please accept/upvote it as you see fit – jimmont Sep 08 '22 at 16:59