Skip to content

Instantly share code, notes, and snippets.

@nicloay
Created February 5, 2014 17:59
Show Gist options
  • Save nicloay/8829596 to your computer and use it in GitHub Desktop.
Save nicloay/8829596 to your computer and use it in GitHub Desktop.
Export adobe illustrator visible layers to png files with the same layer name structure (layers->sublayers becomes folder->subfolders)
// Export Layers as PNG files with original layer structure
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);
}
@eharve4
Copy link

eharve4 commented Aug 23, 2016

How would this be edited to retain artboard sizes vs. snapping to the layers artwork dimensions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment