Skip to content

Instantly share code, notes, and snippets.

@feedmac
Created February 9, 2016 13:33
Show Gist options
  • Save feedmac/49f84bb14c76c35e825f to your computer and use it in GitHub Desktop.
Save feedmac/49f84bb14c76c35e825f to your computer and use it in GitHub Desktop.
Photoshop JS script for quickly exporting selection to PNG.
app.displayDialogs = DialogModes.NO;
var pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.compression = 9;
pngSaveOptions.interlaced = false;
var hasSelection;
var docRef;
var artLayer;
var width = app.activeDocument.width;
var height = app.activeDocument.height;
try {
hasSelection = !!app.activeDocument.selection.bounds;
} catch (err) {
hasSelection = false;
}
if (hasSelection) {
//copy merged
app.activeDocument.selection.copy(true);
//create new RGB document with transperant background
docRef = app.documents.add(width, height, 72, null, NewDocumentMode.RGB, DocumentFill.TRANSPARENT)
artLayer = docRef.paste();
//crop the image to pasted bounds
docRef.crop(artLayer.bounds);
} else {
docRef = app.activeDocument;
}
var file = File.saveDialog("Export as PNG to...");
if (file && ((file.exists && confirm("Overwrite " + file +"?")) || !file.exists)) {
docRef.saveAs(file, pngSaveOptions, !hasSelection, Extension.LOWERCASE);
if (hasSelection) {
docRef.close(SaveOptions.DONOTSAVECHANGES);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment