Combine ungrouped layers
How can I combine layers that are out of group using scripts? Thanks.
How can I combine layers that are out of group using scripts? Thanks.
Thanks for the script, it does what I want, but what I really want is for it to combine all the layers that don't belong to a group, regardless of whether they are before or after any group.
By @Delta2487
¯\_(ツ)_/¯
EDIT: This is what you were really wanting but didn't properly describe:
1) Turn visibility of all layers off
2) Turn visibility on for all layers EXCEPT layer groups
3) Merge visible layers
4) Turn visibility of all layers on
5) Deselect all layers
/*
Merge Ungrouped Layers.jsx
Stephen Marsh
v1.0 - 20th November 2025: Initial release
https://community.adobe.com/t5/photoshop-ecosystem-discussions/select-and-combine-layers-outside-of-group/td-p/15597925
*/
#target photoshop
app.activeDocument.suspendHistory("Merge Ungrouped Layers", "processLayers()");
function processLayers() {
if (!app.documents.length) {
alert("A document must be open to run this script!");
return;
}
var doc = app.activeDocument;
// Get the top ungrouped layer name
var rootLayers = doc.layers;
var topArtLayer = null;
for (var i = 0; i < rootLayers.length; i++) {
if (rootLayers[i].typename === "ArtLayer") {
topArtLayer = rootLayers[i];
break;
}
}
var targetLayerName = topArtLayer.name;
// Add a new dummy layer to the top of the stack
doc.artLayers.add()
// Deselect all layers
app.runMenuItem(stringIDToTypeID('selectNoLayers'));
// Turn visibility of all layers off
setAllVisibility(doc.layers, false);
// Turn visibility on for layers EXCEPT LayerSet groups
showNonGroupLayers(doc.layers);
// Merge visible layers
doc.mergeVisibleLayers();
// Rename the layer
doc.activeLayer.name = targetLayerName;
// Turn visibility of all layers on
setAllVisibility(doc.layers, true);
// Deselect all layers
app.runMenuItem(stringIDToTypeID('selectNoLayers'));
///// Helper functions /////
// Recursively toggle visibility for all layers
function setAllVisibility(layers, state) {
for (var j = 0; j < layers.length; j++) {
try {
layers[j].visible = state;
} catch (e) {}
if (layers[j].typename === "LayerSet") {
setAllVisibility(layers[j].layers, state);
}
}
}
// Turn visibility ON for non-group layers
function showNonGroupLayers(layers) {
for (var k = 0; k < layers.length; k++) {
var lyr = layers[k];
if (lyr.typename === "ArtLayer") {
try { lyr.visible = true; } catch (e) {}
} else if (lyr.typename === "LayerSet") {
// leave group visibility OFF
showNonGroupLayers(lyr.layers);
}
}
}
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.