Skip to content

Instantly share code, notes, and snippets.

@seanriceaz
Last active May 18, 2016 17:31
Show Gist options
  • Save seanriceaz/5fb281362c0e03142455cc8d0b5f5272 to your computer and use it in GitHub Desktop.
Save seanriceaz/5fb281362c0e03142455cc8d0b5f5272 to your computer and use it in GitHub Desktop.
This script cycles through files in a directory (hard coded) and converts them to thumbnail png's.
#target photoshop
// This script cycles through files in a directory (hard coded) and converts them to thumbnail png's.
// Only currently works on portrait orientation images.
app.bringToFront();
app.preferences.rulerUnits = Units.PIXELS;
// A hard coded path to a directory 'pc style'
var folderToProcess = 'h:\\source\\file';
var processFolder = Folder(folderToProcess);
// Use folder object get files function with mask 'a reg ex'
var fileList = processFolder.getFiles(/\.(jpg|tif|psd|eps|png)$/i);
// Loop through files
for (var i = 0; i < fileList.length; i++) {
// Only process the returned file objects
// The filter 'should' have missed out any folder objects
if (fileList[i] instanceof File) {
open(fileList[i]);
var srcDoc = activeDocument;
var fileName = srcDoc.name.replace(".psd","");
//300 px
srcDoc.resizeImage(300,null,72,ResampleMethod.BICUBIC);
//make canvas square
srcDoc.resizeCanvas(300,300);
var fileNamePath = folderToProcess+"\\thumbs\\" + fileName+"-300"+".png";
SavePNG(fileNamePath);
//100 px square
srcDoc.resizeImage(100,100,72,ResampleMethod.BICUBIC);
fileNamePath = folderToProcess+"\\thumbs\\" + fileName+"-100"+".png";
SavePNG(fileNamePath);
//50 px square
srcDoc.resizeImage(50,50,72,ResampleMethod.BICUBIC);
fileNamePath = folderToProcess+"\\thumbs\\" + fileName+"-50"+".png";
SavePNG(fileNamePath);
//Close without saving
srcDoc.close(SaveOptions.DONOTSAVECHANGES);
}
}
function SavePNG(saveFile){
var pngOpts = new ExportOptionsSaveForWeb;
pngOpts.format = SaveDocumentType.PNG;
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.interlaced = false;
pngOpts.quality = 100;
activeDocument.exportDocument(new File(saveFile),ExportType.SAVEFORWEB,pngOpts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment