Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to Export Layers as PNG maintaining Artboard dimension

New Here ,
Jun 03, 2020 Jun 03, 2020

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

 

TOPICS
Import and export , Scripting
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jun 03, 2020 Jun 03, 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

Translate
New Here , Jun 04, 2020 Jun 04, 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.D
...
Translate
Adobe
Community Expert ,
Jun 03, 2020 Jun 03, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2020 Jun 03, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 04, 2020 Jun 04, 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..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 17, 2024 Jul 17, 2024
LATEST

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.");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines