Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Export folder of AI, PNG, or PSD files to PNG w/ transparency
// Source: https://gist.github.com/pixelbacon/7754cba64125b0240504
// Inspired by:
// https://github.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx
// http://blog.wp.weightpoint.se/2012/02/02/batch-export-photoshop-psd-files-to-png-the-right-way/
// To use open Script via Photoshop OR File > Scripts > Browse
#target "photoshop"
if (BridgeTalk.appName == "photoshop")
{
app.bringToFront;
var inputFolder = Folder.selectDialog("Select the folder that contains the files for export:");
var outputFolder = Folder.selectDialog("Select the folder to output to:");
if (inputFolder != null) {
var fileList = inputFolder.getFiles (/\.(psd|png|ai)$/i);
var maxPixels = prompt ("Maximum height & width for exported files:", "2000");
var desiredWidth = prompt ("Desired WIDTH for exported files:", maxPixels);
var desiredHeight = prompt ("Desired HEIGHT for exported files:", maxPixels);
for (var i = 0; i < fileList.length; i++) {
if (fileList [i] instanceof File) {
var document = open (fileList [i]);
var documentName = fileList [i].name.slice (0, -4);
while (app.documents.length) {
var newFile = new File (decodeURI (outputFolder) + "/" + documentName + ".png");
var max = maxPixels + " pixels";
// By flattening we lose transparency
// document.flatten ();
if (document.resolution > 72 || (document.width > max || document.height > max)) {
if (document.width > document.height) {
var newWidth = new UnitValue (desiredWidth + " pixels");
var newHeight = new UnitValue (desiredHeight + " pixels");
} else {
var newWidth = new UnitValue (desiredHeight + " pixels");
var newHeight = new UnitValue (desiredWidth + " pixels");
}
document.resizeImage (newWidth, newHeight, 72);
}
exportOptions = new ExportOptionsSaveForWeb ();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.transparency = true;
exportOptions.PNG8 = false;
// exportOptions.format = SaveDocumentType.JPEG;
// exportOptions.quality = 80;
document.exportDocument (newFile, ExportType.SAVEFORWEB, exportOptions);
document.close (SaveOptions.DONOTSAVECHANGES);
}
}
if (i == fileList.length - 1) {
alert ("All the files have been exported.");
}
}
}
}
@kermes-vermilio

This comment has been minimized.

Copy link

kermes-vermilio commented Nov 13, 2015

Hi,

I tried to use this script to resize a bunch of png's, but when I ran it it didn't seem to keep the aspect ratio (meaning images were coming out squashed).

I had a look and modified your script as follows:

if (document.resolution > 72 || (document.width > max || document.height > max)) {
var newWidth = new UnitValue (desiredWidth + " pixels");
var newHeight = new UnitValue ((document.height*desiredWidth/document.width)+ " pixels");
document.resizeImage (newWidth, newHeight, 72);
}

Essentially this will set the image width to be whatever you choose as your "desired width" and then resizes the height proportionally to the chosen width.

This could be used in a similar fashion to fix heights, too.

:-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.