Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoSoyPhil/9d3581a2db24d47e6d63a780de4ba61a to your computer and use it in GitHub Desktop.
Save YoSoyPhil/9d3581a2db24d47e6d63a780de4ba61a to your computer and use it in GitHub Desktop.
Photoshop script to export for web - going through folders recursively and saves in the source folder inside a new folder "Optimized"
// Photoshop export for web - going through folders recursively
function getOutputFolder(file) {
var parentFolder = file.parent;
var outputFolder = new Folder(parentFolder + "/Optimized");
if (!outputFolder.exists) {
outputFolder.create();
}
return outputFolder;
}
function processFile(file) {
if (file instanceof File && file.name.match(/\.(jpg|jpeg|png|gif)$/i)) {
var doc = open(file)
var outputFolder = getOutputFolder(file);
var options = new ExportOptionsSaveForWeb()
var fileName = file.name
var fileExtension = fileName.split(".").pop().toLowerCase()
switch (fileExtension) {
case "jpg":
case "jpeg":
options.format = SaveDocumentType.JPEG
options.quality = 60 // Adjust for your needs
break;
case "png":
options.format = SaveDocumentType.PNG
options.PNG8 = false
options.transparency = true
break;
case "gif":
options.format = SaveDocumentType.COMPUSERVEGIF
break;
}
var saveFile = new File(outputFolder + "/" + fileName)
doc.exportDocument(saveFile, ExportType.SAVEFORWEB, options)
doc.close(SaveOptions.DONOTSAVECHANGES)
}
}
function processFolder(folder) {
var files = folder.getFiles();
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof Folder) {
processFolder(file); // Recursive call for subfolders
} else {
processFile(file); // Process the file
}
}
}
// Choose input-folder
var inputFolder = Folder.selectDialog("Choose a folder")
if (inputFolder != null) {
processFolder(inputFolder);
}
@YoSoyPhil
Copy link
Author

screenshot

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