Skip to main content
mayurrajv_007
New Participant
June 4, 2020
Answered

Script to Export Layers as PNG maintaining Artboard dimension

  • June 4, 2020
  • 3 replies
  • 1811 views

Artboard 1

->layer 1

->layer 2

 

finding a script to export layer 1 and 2 as png with dimension of Artboard 1.

tried this one, it worked but exported layers sizes are diffrent..

var options = new ExportOptionsPNG24();
var doc = app.activeDocument;
$.writeln("trying to save");

var newdoc = app.documents.add(doc.documentColorSpace, doc.width, doc.height);
newdoc.artboards[0].artboardRect = doc.artboards[0].artboardRect;

saveSubLayers(doc, "");

newdoc.close(SaveOptions.DONOTSAVECHANGES);

function saveSubLayers(node, path){
    for (var i=0; i<node.layers.length; i++){
        var layer = node.layers[i];
        if (!layer.visible) 
                continue;
        if (layer.layers.length > 0){
             saveSubLayers(layer, path+"/"+layer.name);
        } else {
            saveLayer(layer, path);
        }        
    }
}

function saveLayer(layer, path){
    newdoc.layers[0].remove();
    var newlayer = newdoc.layers[0];
	for (var ii = layer.pageItems.length - 1; ii >= 0; ii--)
		layer.pageItems[ii].duplicate(newlayer, ElementPlacement.PLACEATBEGINNING);

	new Folder(doc.path + path).create();
	newdoc.exportFile(new File(doc.path + path + "/" + layer.name + ".png"), ExportType.PNG24, options);
}

 

This topic has been closed for replies.
Correct answer mayurrajv_007

https://gist.github.com/TomByrne/7816376

this one worked like a charm,

but i've figured out missing parameters from existing script and that also worked fine,

var options = new ExportOptionsPNG24();
options.artBoardClipping = true; //this line

var doc = app.activeDocument;
$.writeln("trying to save");

var newdoc = app.documents.add(doc.documentColorSpace, doc.width, doc.height);
newdoc.artboards[0].artboardRect = doc.artboards[0].artboardRect;


saveSubLayers(doc, "");

newdoc.close(SaveOptions.DONOTSAVECHANGES);

function saveSubLayers(node, path){
    for (var i=0; i<node.layers.length; i++){
        var layer = node.layers[i];
        if (!layer.visible) 
                continue;
        if (layer.layers.length > 0){
             saveSubLayers(layer, path+"/"+layer.name);
        } else {
            saveLayer(layer, path);
        }        
    }
}


function saveLayer(layer, path){
    newdoc.layers[0].remove();
    var newlayer = newdoc.layers[0];
	for (var ii = layer.pageItems.length - 1; ii >= 0; ii--)
		layer.pageItems[ii].duplicate(newlayer, ElementPlacement.PLACEATBEGINNING);

	new Folder(doc.path + path).create();
	newdoc.exportFile(new File(doc.path + path + "/" + layer.name + ".png"), ExportType.PNG24, options);
}

 but again github one has more control over exported content so yeah, gotta use that.. thanks for helping me out..

3 replies

New Participant
July 17, 2024

couldn't get these to work in artboard size but got this one that works like a charm!

 

// Save each layer as a separate PNG file with artboard size
var doc = app.activeDocument;
var artboard = doc.artboards[0];
var exportFolder = Folder.selectDialog("Select the folder to export PNGs");

// Function to export a layer
function exportLayer(layer, fileName) {
// Hide all layers
for (var i = 0; i < doc.layers.length; i++) {
doc.layers[i].visible = false;
}

// Show only the current layer
layer.visible = true;

// Export options
var exportOptions = new ExportOptionsPNG24();
exportOptions.artBoardClipping = true;
exportOptions.horizontalScale = 100.0;
exportOptions.verticalScale = 100.0;
exportOptions.transparency = true;

var file = new File(exportFolder + "/" + fileName + ".png");

// Export the layer
doc.exportFile(file, ExportType.PNG24, exportOptions);
}

// Export each layer
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers[i];
exportLayer(layer, layer.name);
}

// Restore all layers visibility
for (var i = 0; i < doc.layers.length; i++) {
doc.layers[i].visible = true;
}

alert("Exporting layers as PNG files is complete.");

Scott Falkner
Community Expert
June 4, 2020

I don’t know sweet FA about scripting, but I did spring for Smart Layer Export. It's very customizable and versatile.

https://exchange.adobe.com/creativecloud.details.2955.smart-layer-export.html

 

There’s also a free version. Less versatile and customizable, but perhaps better suited to your situation.

https://gist.github.com/TomByrne/7816376

mayurrajv_007
mayurrajv_007AuthorCorrect answer
New Participant
June 4, 2020

https://gist.github.com/TomByrne/7816376

this one worked like a charm,

but i've figured out missing parameters from existing script and that also worked fine,

var options = new ExportOptionsPNG24();
options.artBoardClipping = true; //this line

var doc = app.activeDocument;
$.writeln("trying to save");

var newdoc = app.documents.add(doc.documentColorSpace, doc.width, doc.height);
newdoc.artboards[0].artboardRect = doc.artboards[0].artboardRect;


saveSubLayers(doc, "");

newdoc.close(SaveOptions.DONOTSAVECHANGES);

function saveSubLayers(node, path){
    for (var i=0; i<node.layers.length; i++){
        var layer = node.layers[i];
        if (!layer.visible) 
                continue;
        if (layer.layers.length > 0){
             saveSubLayers(layer, path+"/"+layer.name);
        } else {
            saveLayer(layer, path);
        }        
    }
}


function saveLayer(layer, path){
    newdoc.layers[0].remove();
    var newlayer = newdoc.layers[0];
	for (var ii = layer.pageItems.length - 1; ii >= 0; ii--)
		layer.pageItems[ii].duplicate(newlayer, ElementPlacement.PLACEATBEGINNING);

	new Folder(doc.path + path).create();
	newdoc.exportFile(new File(doc.path + path + "/" + layer.name + ".png"), ExportType.PNG24, options);
}

 but again github one has more control over exported content so yeah, gotta use that.. thanks for helping me out..

Scott Falkner
Community Expert
June 4, 2020

I don’t know sweet FA about scripting, but I did spring for Smart Layer Export. It's very customizable and versatile.

https://exchange.adobe.com/creativecloud.details.2955.smart-layer-export.html