Skip to content

Instantly share code, notes, and snippets.

@livingston
Created March 16, 2010 13:25
Show Gist options
  • Save livingston/333955 to your computer and use it in GitHub Desktop.
Save livingston/333955 to your computer and use it in GitHub Desktop.
Photoshop Script for Optimizing Images for the Web - Saves images as optimized jpeg
// Photoshop Script for Optimizing Images for the Web - Saves images as optimized jpeg
// Written by: Livingston Samuel
// Version 1.0.0
// Required Adobe Photoshop CS 2 and above
//Enable double clicking on Mac Finder & Windows Explorer
#target Photoshop
//Bring app to front
app.bringToFront()
// Backup the current Photoshop preferences
var startDisplayDialogs = app.displayDialogs,
startRulerUnits = app.preferences.rulerUnits;
//Set Photoshop to use pixels and display no dialogs
app.displayDialogs = DialogModes.NO;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
// Get the Source folder
var inputFolder = Folder.selectDialog("Select the Source folder for Images to be optimized");
// Get the Destination folder
if (!!inputFolder) {
var outputFolder = Folder.selectDialog("Select a folder to store the generated Optimized images");
}
if (!!inputFolder && !!outputFolder) {
processImagesInFolder(inputFolder);
}
function processImagesInFolder(folder) {
var fileList = folder.getFiles();
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof File && (file.name.match(/\.jpg$/i) || file.name.match(/\.png$/i) || file.name.match(/\.gif$/i)) && file.hidden == false) { // The fileList includes both folders and files so open only files
open(file); // Open the file
//Get Current File's Name
var imgfileName = file.name.split('.')
//Set Destination file path
var saveFile = new File(outputFolder + "/" + imgfileName[0] + ".jpg");
saveFile.remove(); // Remove any exisiting file with same name before saving
saveAsOptimizedJPEG(saveFile,60); // Saving the file for web with 60% JPEG optimization
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); //Close the File
} else if (file instanceof Folder) {
processFolder(file); // Traverse subfolders
}
}
}
function saveAsOptimizedJPEG(saveFile,jpegQuality) { // Optimize Image for Web
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality;
app.activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
// Reset Photoshop preferences to original vlaues
app.displayDialogs = startDisplayDialogs;
app.preferences.rulerUnits = startRulerUnits;
@josefanostylus
Copy link

Does this still work in Photoshop 2020 ?

@alverick
Copy link

Does this still work in Photoshop 2020 ?

works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment