Skip to content

Instantly share code, notes, and snippets.

@amadornes
Created August 8, 2021 16:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save amadornes/32bff3b3ef53a97a487c4cce7551550b to your computer and use it in GitHub Desktop.
// Exports a saved multi-layered file as a .png in the same directory.
function exportPNG() {
// Confirm the document has already been saved and so has a path to use
try {
app.activeDocument.save();
} catch(e) {
alert("Could not export PNG as the document is not saved.\nPlease save and try again.")
return
}
// Store the active doc handle in variable
var doc = app.activeDocument;
// Check there is at least 1 visible layer.
var foundVisible = false;
for (i = 0; i < doc.layers.length; i++) {
if (doc.layers[i].visible) {
foundVisible = true;
break;
}
}
if (!foundVisible) return;
// Set up PNG save options.
pngOptions = new PNGSaveOptions();
pngOptions.compression = 9;
pngOptions.interlaced = false;
// Set up destination path.
savePath = File(doc.path + "/" + doc.name.replace(/\.[^\.]+$/, '.png'));
// Save!
doc.saveAs(savePath, pngOptions, true, Extension.LOWERCASE);
// Just in case, make sure the active document is the orignal one.
app.activeDocument=doc;
}
exportPNG();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment