The script, Batch_Merge_Groups_into_Layers.jsx, is designed to process and manipulate the layer structure of a Photoshop document by merging layer groups (or layer sets) into individual layers. Here’s what the script is good for and its key purposes:
- Consolidating Complex Layer Structures
• Many Photoshop documents have multiple nested groups or organized layer sets to manage different elements separately. This script automates the process of combining these group layers into a single layer.
• By merging groups, it helps reduce complexity in highly layered documents, making further editing or exporting simpler. - Streamlining Workflow for Final Output
• Once the design work is complete, it’s often a good idea to reduce the number of layers to prepare the file for print, web, or other final outputs.
• This script efficiently “flattens” grouped layers, which can help ensure that styles, effects, and blending modes are correctly applied without the overhead of managing multiple layers. - Automated Batch Processing
• The script can operate on multiple selected target layers, making it useful for batch processing. This means you can merge many groups at once rather than doing it manually for each group. - Enhancing Document Management
• With the ability to merge groups into single layers, the script can be valuable when trying to clean up a document—especially if you’re dealing with many layers that can be consolidated after certain effects or adjustments have been applied.
In summary, this script is particularly useful when you need to simplify a Photoshop document’s layer structure by merging various groups or sets into individual layers, thereby streamlining the editing process, preparing the file for final output, or automating repetitive layer management tasks.
JavaScript
// Converts a string to a unique Photoshop type ID
function sTT(v) {
return stringIDToTypeID(v);
}
// Set the script’s output level (0 means no extended logging)
$.level = 0
// Returns a new ActionReference object (used to refer to document or layer properties)
function R() {
return ref = new ActionReference();
}
arr = [] // Global array to store layer identifiers
// Helper function 'DoL' selects a layer using an enumerated ordinal target (like "targetEnum")
function DoL(v) {
R().putEnumerated(sTT(v), sTT('ordinal'), sTT('targetEnum'));
}
// 'bGrnd' selects a layer by its index (v)
function bGrnd(v) {
R().putIndex(sTT('layer'), v);
}
// 'SC' checks the layer section value (e.g., whether a layer is a group start or content)
// It uses executeActionGet to get the property and then compares the result.
function SC(v) {
return typeIDToStringID(
(dsc = executeActionGet(ref)).getEnumerationValue(sTT(lS = 'layerSection'))
) == lS + v;
}
// 'sNL' deselects the current layers. It uses the special action 'selectNoLayers'.
function sNL() {
DoL('layer');
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref);
executeAction(sTT('selectNoLayers'), dsc, DialogModes.NO);
}
// 'mLN' merges the layer specified by its layer identifier (v).
// It selects that layer, ensures it’s visible (if needed), and then executes a merge operation.
function mLN(v) {
R().putIdentifier(sTT('layer'), v);
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref);
dsc.putBoolean(sTT('makeVisible'), true);
executeAction(sTT('select'), dsc, DialogModes.NO);
executeAction(sTT('mergeLayersNew'), undefined, DialogModes.NO);
}
// 'prcs' (process) function that examines a layer (given by index v1) and, based on its section type,
// either stores its layer ID or merges it immediately.
// The parameter v2 determines whether the layer should be queued or merged.
function prcs(v1, v2) {
bGrnd(idx = v1);
lID = executeActionGet(ref).getInteger(sTT('layerID'));
if (SC('Start')) { // Check if current layer marks the start of a layer group
bGrnd(idx - 1);
if (SC('Content')) {
// If the previous layer is a content layer,
// add the current layer ID to the global array if v2==true.
v2 ? arr.push(lID) : (sNL(), mLN(lID));
}
}
}
try {
// In Photoshop, layer 1 might be the background. Check if the first layer is a background.
R().putIdentifier(sTT('layer'), 1);
bG = !(executeActionGet(ref).getBoolean(sTT('background')));
}
catch (err) {
// If an error occurs, assume there is a background layer.
bG = 1;
}
// Prepare to use the target layers property
DoL('document');
tL = sTT('targetLayers');
// If the document contains target layers (multiple layers selected or exists in a list)
if ((dsc = executeActionGet(ref)).hasKey(tL)) {
// Process all layers in the list
for (lst = dsc.getList(tL), i = 0; i < lst.count;) {
prcs(lst.getReference(i++).getIndex() + bG, true);
}
// After processing, deselect layers and merge the ones queued in 'arr'
sNL();
for (i = 0; i < arr.length; i++) {
mLN(arr[i]);
}
} else {
// If there are no target layers, process the current itemIndex.
DoL('layer');
prcs(executeActionGet(ref).getInteger(sTT('itemIndex')) - !bG, false);
}