Skip to content

Instantly share code, notes, and snippets.

@advantis
Created May 15, 2013 07:06
Show Gist options
  • Save advantis/5582134 to your computer and use it in GitHub Desktop.
Save advantis/5582134 to your computer and use it in GitHub Desktop.
Trim and export visible layer(s) as two PNG images (standard and retina)
var fileName = prompt('Image name', 'image');
if (null != fileName)
{
var selection = activeDocument.selection;
try
{
selection.copy(true);
}
catch (error)
{
selection.selectAll();
selection.copy(true);
}
var width = selection.bounds[2].value - selection.bounds[0].value;
var height = selection.bounds[3].value - selection.bounds[1].value;
documents.add(width, height, 72, "Untitled", NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
activeDocument.paste(false);
activeDocument.trim(TrimType.TRANSPARENT);
var options = new ExportOptionsSaveForWeb();
options.format = SaveDocumentType.PNG;
options.PNG8 = false;
var file;
file = new File('~/Desktop/' + fileName + '@2x.png');
activeDocument.exportDocument (file, ExportType.SAVEFORWEB, options);
activeDocument.resizeImage('50%', '50%', 72, ResampleMethod.BICUBICSHARPER);
file = new File('~/Desktop/' + fileName + '.png');
activeDocument.exportDocument (file, ExportType.SAVEFORWEB, options);
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment