Last active
November 13, 2023 14:49
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Author
YoSoyPhil
commented
Nov 13, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment