Skip to content

Instantly share code, notes, and snippets.

@v0d1ch
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save v0d1ch/af6a5595cf86d534a880 to your computer and use it in GitHub Desktop.

Select an option

Save v0d1ch/af6a5595cf86d534a880 to your computer and use it in GitHub Desktop.
jsx photoshop to save every png file in subfolders as jpg
var traverseFolder = function(path)
{
// Create new folder object based on path string
var folder = new Folder(path);
// Get all files in the current folder
var files = folder.getFiles();
// Loop over the files in the files object
for (var i = 0; i < files.length; i++)
{
// Check if the file is an instance of a file
// else call the traverse folder recursively with the current folder as an argument
if (files[i] instanceof File)
{
// Convert the file object to a string for matching purposes (match only works on String objects)
var fileString = String(files[i]);
// Check if the file contains the right extension
if (fileString.match(/.(png)$/))
{
// Do something if the file matches
var fileRef = new File(fileString);
var docRef = app.open (fileRef);
var Name = fileString.replace(/\.png$/, '.jpg');
var saveFile = File(Name);
SaveForWeb(saveFile,60);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
else
{
// Do something if the file doesn't match
alert(fileString + ' bad file');
}
}
else
{
alert('passing folder: ' + files[i]);
// Call method recursively with the current folder as an argument
traverseFolder(files[i]);
}
}
}
traverseFolder('~/desktop/ggg');
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment