Skip to content

Instantly share code, notes, and snippets.

@advantis
Created April 12, 2012 17:29
Show Gist options
  • Save advantis/2369378 to your computer and use it in GitHub Desktop.
Save advantis/2369378 to your computer and use it in GitHub Desktop.
Export PS top-level layer groups as PNG images
// enable double clicking from the Macintosh Finder or the Windows Explorer
#target photoshop
const ScriptResult =
{
CANCEL: 'cancel' // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
};
function exportGroups(document, folder, retina)
{
var groups = document.layerSets;
while (0 < groups.length)
{
var group = groups[0];
if (group.visible)
{
var layer = group.merge();
layer.copy(false);
var tmpDoc = documents.add(document.width, document.height, 72, 'Untitled', NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
tmpDoc.paste(false);
tmpDoc.trim(TrimType.TRANSPARENT);
var file = new File(folder + '/' + layer.name + (retina ? '@2x.png' : '.png'));
tmpDoc.saveAs (file, new PNGSaveOptions(), true);
tmpDoc.close(SaveOptions.DONOTSAVECHANGES);
}
else
{
group.remove();
}
}
}
function main()
{
if (0 == documents.length)
{
alert('No open documents');
return ScriptResult.CANCEL;
}
var folder = Folder.selectDialog('Select destination folder', '~');
if (null == folder) return ScriptResult.CANCEL;
var document;
document = activeDocument.duplicate();
exportGroups(document, folder, true);
document.close(SaveOptions.DONOTSAVECHANGES);
document = activeDocument.duplicate();
document.resizeImage('50%', '50%');
exportGroups(document, folder, false);
document.close(SaveOptions.DONOTSAVECHANGES);
alert('Export finished successfully');
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment