Skip to main content
Inspiring
November 25, 2020
Answered

merge all layersSets

  • November 25, 2020
  • 1 reply
  • 528 views

Hi, 

Iv' got script that merge or flatten, all layer in layerSet (Group), but sometimes i've got "merge layer is not available". There is another way, to do a "merge group" ?

 

function mergeAllGroups(){
  shotlayerssets = app.activeDocument.layerSets;
  for (i=shotlayerssets.length; i>0; i-- )
    {
      shotlayerssets[i-1].merge();
    }
}

 thank you

This topic has been closed for replies.
Correct answer jeremybep

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);
  }
}

1 reply

c.pfaffenbichler
Community Expert
Community Expert
November 25, 2020

Could you please post a screenshot taken at failing time with the pertinent Panels (Toolbar, Layers, History, Options Bar, …) visible? 

jeremybepAuthor
Inspiring
November 26, 2020

Hi, 

During photoshop batch processing script, i load this script, an then on some psd file i'v got this error:

 

I will check again if there is not a difference between psd file, because it is strange

Maybe this help,

In this script i've pasted code of http://bereza.cz/ps/ (Delete empty layers faster), then the paste cade above.

 

 

jeremybepAuthor
Inspiring
November 26, 2020

ok, after trying with lot of psd file. I'v finally found.

When i call script from bereza, i'v got this pop up. When i don't call it it work...

So, there is no another way to delete empty folder ?