@Liu254468142lhe
A basic conversion of all root/top-level groups to artboards will simply stack each artboard, one on top of the other*. This script is a bit of a cheat, but it works for me. Extra code could be added to check if the layer group contained "Post" in its name:
/*
All Root-Top-Level Groups to Artboards.jsx
v1.0 15th April 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/help-convert-a-layer-group-to-an-artboard/td-p/14552904
NOTE: This script makes use of features found in later versions of Photoshop. Tested in version 2024 and 2021, errors may occur in earlier versions.
*/
try {
///// Pre-loop /////
// Ensure that a layer is selected before starting the loop
app.activeDocument.activeLayer = app.activeDocument.activeLayer.layers[0];
///// Loop over the root/top-level layer sets /////
for (var i = 0; i < activeDocument.layerSets.length; i++) {
activeDocument.activeLayer = activeDocument.layers[i];
if (isGroup() === true) {
groupToArtboard();
}
function isGroup() {
// modified from a script by greless with hints from jazz-y!
// returns true or false
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID('layer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));
var options = executeActionGet(r);
return options.hasKey(stringIDToTypeID('layerSection')) && !options.hasKey(stringIDToTypeID('framedGroup')) && !options.hasKey(stringIDToTypeID('artboard')); // test for the required key
}
function groupToArtboard() {
// Convert the group to a SO to "safely isolate it" for the artboard conversion
executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
// Edit the SO
executeAction(stringIDToTypeID("placedLayerEditContents"), undefined, DialogModes.NO);
// Convert the group to an artboard
var idselect = stringIDToTypeID("select");
var desc751 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref90 = new ActionReference();
var idmenuItemClass = stringIDToTypeID("menuItemClass");
var idmenuItemType = stringIDToTypeID("menuItemType");
var idartboardFromLayersEvent = stringIDToTypeID("artboardFromLayersEvent");
ref90.putEnumerated(idmenuItemClass, idmenuItemType, idartboardFromLayersEvent);
desc751.putReference(idnull, ref90);
executeAction(idselect, desc751, DialogModes.NO);
// Close the SO saving changes
app.activeDocument.close(SaveOptions.SAVECHANGES);
// Convert the SO to layers
var idselect = stringIDToTypeID("select");
var desc816 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref95 = new ActionReference();
var idmenuItemClass = stringIDToTypeID("menuItemClass");
var idmenuItemType = stringIDToTypeID("menuItemType");
var idplacedLayerConvertToLayers = stringIDToTypeID("placedLayerConvertToLayers");
ref95.putEnumerated(idmenuItemClass, idmenuItemType, idplacedLayerConvertToLayers);
desc816.putReference(idnull, ref95);
executeAction(idselect, desc816, DialogModes.NO);
}
}
///// Post-loop /////
// Select all layers
app.runMenuItem(stringIDToTypeID('selectAllLayers'));
// Unlock the nested artboards
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
descriptor2.putBoolean(s2t("protectNone"), true);
descriptor.putObject(s2t("layerLocking"), s2t("layerLocking"), descriptor2);
executeAction(s2t("applyLocking"), descriptor, DialogModes.NO);
// Deselect all layers
app.runMenuItem(stringIDToTypeID('selectNoLayers'));
} catch (e) {
alert("Error!" + "\r" + e + ' ' + e.line);
}
* I find it hard to script the distribution and gap of artboards (horizontally, vertically, in a grid/array etc). I am not aware of any scripts that can auto-arrange and or space out all artboards into such a layout (such a script would be a great tool to have, I have never been able to get the distribute artboards feature to work for this task).
... View more