Reputation: 1
The script I tried works, but it's only partially accurate because it only exports the first two selected layers/groups in the layer panel as individual PNGs. Also, for the script to work as intended, the visibility of the selected layers needs to be disabled except for the topmost selected layer. I want all the selected layers to be exported as individual PNGs. Regardless of their visibility in the layers panel.
Simply put I want to take a group merge its contents and export as a PNG with transparency then move on to the next group......
any help would be appreciated!
function main() {
if (!documents.length) return;
var doc = activeDocument;
var oldPath = activeDocument.path;
for (var a = 0; a < doc.layerSets.length; a++) {
var layerSetName = doc.layerSets[a].name;
var targetLayer = getLayerByName(layerSetName);
if (targetLayer) {
activeDocument.activeLayer = targetLayer;
duplicateLayer();
activeDocument.mergeVisibleLayers();
// Assuming you want to save each group with its name
var saveFile = new File(oldPath + "/" + layerSetName + ".png");
SavePNG(saveFile);
}
}
}
function SavePNG(saveFile) {
var pngOpts = new ExportOptionsSaveForWeb;
pngOpts.format = SaveDocumentType.PNG;
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.interlaced = false;
pngOpts.quality = 100;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, pngOpts);
}
function duplicateLayer() {
var desc143 = new ActionDescriptor();
var ref73 = new ActionReference();
ref73.putClass(charIDToTypeID('Dcmn'));
desc143.putReference(charIDToTypeID('null'), ref73);
desc143.putString(charIDToTypeID('Nm '), activeDocument.activeLayer.name);
var ref74 = new ActionReference();
ref74.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc143.putReference(charIDToTypeID('Usng'), ref74);
executeAction(charIDToTypeID('Mk '), desc143, DialogModes.NO);
}
function getLayerByName(name) {
var layers = activeDocument.layers;
for (var i = 0; i < layers.length; i++) {
if (layers[i].name === name) {
return layers[i];
}
}
return null;
}
main();
Upvotes: 0
Views: 130
Reputation: 6967
Your description is not clear at all. However, the basis of your script is this: You need to loop over ALL layers using a recursive function, then detect if the layer is a group and then merge it before you export it.
// call the source document
var srcDoc = app.activeDocument;
// group layer vegetables
var theLayers = collectAllLayers(app.activeDocument, 0);
// function collect all layers
function collectAllLayers (theParent, level)
{
for (var m = theParent.layers.length - 1; m >= 0; m--)
{
var theLayer = theParent.layers[m];
// apply the function to layersets;
if (theLayer.typename == "LayerSet")
{
//alert(theLayer.name);
// make the current layer active
srcDoc.activeLayer = theLayer;
merge_group();
collectAllLayers(theLayer, level + 1)
}
}
}
function merge_group()
{
// =======================================================
var idMrgtwo = charIDToTypeID( "Mrg2" );
executeAction( idMrgtwo, undefined, DialogModes.NO );
}
Upvotes: 0