Piggybacking off of pixxxel schubser
function MergeAllLayers() {
// exit early if there are no files open
if( !app.documents.length ) return;
var doc = app.activeDocument;
// clear selection
doc.selection = null;
// loop through all layers
var layers = doc.layers;
for( var i = 0, ii = layers.length; i < ii; i++ ) {
// select everything on the current layer
layers.hasSelectedArtwork = true;
app.executeMenuCommand("group");
app.executeMenuCommand("Live Pathfinder Add");
app.executeMenuCommand("expandStyle");
// clear selection
doc.selection = null;
}
}
MergeAllLayers();
Setting the hasSelectedArtwork Layer property equal to true is a fun trick to select everything on a layer, you just have to make sure to clear any other selection first.