What do you use to select a Tool on screen with the cursor? What do you use to organise and move Layers? etc.
Anyway, please try this:
// 2020, use it your own risk;
if (app.documents.length > 0) {
var thedoc = app.activeDocument;
var docName = thedoc.name;
var basename = docName.match(/(.*)\.[^\.]+$/)[1];
var docPath = thedoc.path;
//
deleteHidden ();
mergeGroups (thedoc, []);
//
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = false;
psdOpts.layers = true;
psdOpts.spotColors = true;
thedoc.saveAs((new File(docPath+"/"+basename+"_merged"+".psd")),psdOpts,false);
};
// delete hidden layer;
function deleteHidden () {
try {
var id26 = charIDToTypeID( "Dlt " );
var desc6 = new ActionDescriptor();
var id27 = charIDToTypeID( "null" );
var ref6 = new ActionReference();
var id28 = charIDToTypeID( "Lyr " );
var id29 = charIDToTypeID( "Ordn" );
var id30 = stringIDToTypeID( "hidden" );
ref6.putEnumerated( id28, id29, id30 );
desc6.putReference( id27, ref6 );
executeAction( id26, desc6, DialogModes.NO );
}
catch (e) {}
};
// apply layermask;
function applyLayerMask (a) {
try {
app.activeDocument.activeLayer = a;
// =======================================================
var id500 = charIDToTypeID( "Dlt " );
var desc96 = new ActionDescriptor();
var id501 = charIDToTypeID( "null" );
var ref67 = new ActionReference();
var id502 = charIDToTypeID( "Chnl" );
var id503 = charIDToTypeID( "Chnl" );
var id504 = charIDToTypeID( "Msk " );
ref67.putEnumerated( id502, id503, id504 );
desc96.putReference( id501, ref67 );
var id505 = charIDToTypeID( "Aply" );
desc96.putBoolean( id505, true );
executeAction( id500, desc96, DialogModes.NO );
}
catch (e) {}
};
////// function collect all layers //////
function mergeGroups (theParent, allLayers) {
if (!allLayers) {var allLayers = new Array}
else {};
for (var m = theParent.layers.length - 1; m >= 0;m--) {
var theLayer = theParent.layers[m];
// apply the function to layersets;
if (theLayer.typename == "ArtLayer") {
// allLayers.push(theLayer)
}
else {
// allLayers = (collectLayers(theLayer, allLayers))
var theB = theLayer.bounds;
if (theB[0] == theB[2] || theB[1] == theB[3]) {theLayer.remove()}
else {var theLayer = theLayer.merge()};
applyLayerMask (theLayer);
// this line includes the layer groups;
allLayers.push(theLayer);
}
};
return allLayers
};
I only open photoshop, batch process with .atn file.
Finally, i've found another way :
function mergeAllGroups(){
while(app.activeDocument.layerSets.length){
activeDocument.activeLayer = app.activeDocument.layerSets[0];
executeAction(stringIDToTypeID("newPlacedLayer"), new ActionDescriptor(), DialogModes.NO);
activeDocument.activeLayer.rasterize(RasterizeType.ENTIRELAYER);
}
}