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.
š
var g = (ad = activeDocument).activeLayer; if (g.typename == 'LayerSet' && g.layers.length) ad.activeLayer = g.layers[0]; ad.activeLayer = g
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
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?
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)
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://gist.github.com/MarshySwamp/a39e3e1efee41174b3b92ee68c40fcd8
Copy link to clipboard
Copied
OK, so this function will (should?) collapse only the active layer group. It has had limited testing, so use with care:
/*
Collapse Only The Active Layer Group.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/action-or-script-to-expand-unfold-layer-groups/m-p/13077014
Stephen Marsh, 9th August 2022, v1.1
*/
#target photoshop
collapseActiveGroup();
function collapseActiveGroup() {
function main() {
if (activeDocument.activeLayer.typename === "LayerSet") {
// Doc name variable
var origDoc = app.activeDocument.name;
// Toggle the active layer visibility
toggleLayerVisibility(true);
// 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 the visible backward layer
selectForwardORBackwardLayer(false, "backwardEnum");
// Delete the original expanded group
removeGroup();
// Select the visible forward layer
selectForwardORBackwardLayer(false, "forwardEnum");
// Toggle the original active layer visibility
toggleLayerVisibility(true);
} 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 selectForwardORBackwardLayer(makeVisible, forwardORbackward) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
// "forwardEnum" or "backwardEnum"
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t(forwardORbackward));
descriptor.putReference(s2t("null"), reference);
// true or false
descriptor.putBoolean(s2t("makeVisible"), makeVisible);
list.putInteger(15);
descriptor.putList(s2t("layerID"), list);
executeAction(s2t("select"), descriptor, DialogModes.NO);
}
function removeGroup() {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
executeAction(s2t("delete"), descriptor, DialogModes.NO);
}
function toggleLayerVisibility(toggleOptionsPalette) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
list.putReference(reference);
descriptor.putList(s2t("null"), list);
descriptor.putBoolean(s2t("toggleOptionsPalette"), toggleOptionsPalette);
executeAction(s2t("show"), descriptor, DialogModes.NO);
}
}
activeDocument.suspendHistory('Collapse Only The Active Layer Group.jsx', 'main()');
}
Copy link to clipboard
Copied
Update: I believe that the following script may work better than the one that I created above.
https://gist.github.com/MarshySwamp/a39e3e1efee41174b3b92ee68c40fcd8
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.
Copy link to clipboard
Copied