Skip to content

Instantly share code, notes, and snippets.

@Jerakin
Forked from tomekc/ps-export-layers-to-png.jsx
Last active January 6, 2020 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jerakin/56f1bd1e5f0a35b54eb6164251822c55 to your computer and use it in GitHub Desktop.
Save Jerakin/56f1bd1e5f0a35b54eb6164251822c55 to your computer and use it in GitHub Desktop.
Photoshop script that exports to PNG all layers and groups whose names end with ".png".
#target photoshop
// $.level = 2;
/*
* Script by Tomek Cejner (tomek (at) japko dot info)
* based on work of Damien van Holten:
* http://www.damienvanholten.com/blog/export-groups-to-files-photoshop/
*
* My version adds support of nested layer groups,
* and exports single layers in addition to groups.
*
* supports / in files names
* export/file.png will be saved in a subfolder named export
*
*/
function main(){
if(!documents.length) return;
var doc = activeDocument;
var oldPath = activeDocument.path;
scanLayerSets(doc);
function scanLayerSets(el) {
// find layer groups
for(var a=0;a<el.layerSets.length;a++){
if (el.layerSets[a].visible == false) {
// If the layer is hidden we skip it
continue
}
var lname = el.layerSets[a].name;
if (lname.substr(-4) == ".png") {
saveLayer(el.layers.getByName(lname), lname, oldPath, true);
scanLayerSets(el.layerSets[a]);
} else {
// recursive
scanLayerSets(el.layerSets[a]);
}
}
// find plain layers in current group whose names end with .png
for(var j=0; j<el.artLayers.length; j++) {
var name = el.artLayers[j].name;
if (name.substr(-4) == ".png") {
saveLayer(el.layers.getByName(name), name, oldPath, false);
}
}
}
function saveLayer(layer, lname, path, shouldMerge) {
activeDocument.activeLayer = layer;
dupLayers();
if (shouldMerge === undefined || shouldMerge === true) {
activeDocument.mergeVisibleLayers();
}
activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true);
var savePath = path + "/" + lname;
var saveFile= File(savePath);
var split_string = SplitPath(savePath);
var folder = split_string[0];
if (!folder.exists) {
var outFolder = new Folder(folder);
outFolder.create();
}
SavePNG(saveFile);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
};
main();
function SplitPath(input_path) {
var split_string = input_path.split('/');
var file_name = split_string.pop();
new_path = split_string.join('/')
return [new_path, file_name]
};
function dupLayers() {
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 SavePNG(saveFile){
var pngOpts = new PNGSaveOptions();
pngOpts.compression = 0
pngOpts.interlaced = false
activeDocument.saveAs(saveFile, pngOpts, true, Extension.LOWERCASE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment