Skip to main content
Participant
September 8, 2007
Question

Script Needed to work with "Save for Web" Tool

  • September 8, 2007
  • 26 replies
  • 3184 views
I am looking to compress the JPG image files using the Save for Web tool within Photoshop CS2. As you know, this tool yields better results in image quality and file size than the standard Photoshop Save As command.

To this end, I would like to batch process multiple images within multiple sub-folders. Unfortunately, I havent found a process that will allow the use of the Save for Web JPG compression, batch process the JPG files within a sub-folder structure and re-write the images to the same folder.

What I have managed to accomplish up to this point, is create a Photoshop Action incorporating the Save for Web tool. However, the Save for Web menu always prompts to save a file to a specified folder. This seems to be the Catch-22 to the batch processing capabilities. When using Photoshops batch processing feature with this Action, and also enabling include all sub-folders, all the resulting files created are always saved to one specified folder.

I need a script (perhaps Java or Visual Basic) which will run in Photoshop, and allow the use the Save for Web tool (JPG High, Quality 60, Optimized), batch process a series of images contained within a sub-folder structure and then re-write the image files to the same location. Can anyone help with this?
This topic has been closed for replies.

26 replies

Larry G. Schneider
Community Expert
Community Expert
September 11, 2007
Paul,

The InDesign Scripting Forum has a permanent item with a procedure to prepare a script for this forum. It's the fifth item down: JS:Script Poster.
Paul Riggott
Inspiring
September 11, 2007
Yes I see the problem. It how the script was pasted here. The format is not good! It has joined a lot of lines together, one of them was a comment line and as it has joined some more lines to that one the save does not work. If only I knew have to post the code with the correct format! On most sites you can use
 
but not here.
The bit thats causing the problem is :-
//Any other processing commands ...
If you remove this from the code it sould then work.

Paul
Participant
September 11, 2007
Paul;

I successfully created the jsx script and loaded it into Photoshop CS2 ok. When I ran the script, it did manage to open all the jpg image files within the sub-folders, however it did not execute the Save for Web command and then Save. Suggestions?

-- Gordon
Paul Riggott
Inspiring
September 9, 2007
Its a Javascript, so save it with an extention of .jsx
then call it from Photoshop.
File - Scripts - Browse to where you have saved the file.
It will prompt for the top level directory and process all sub-directories. (Hopefully).
Hope that helps Gordon.
Paul.
Participant
September 8, 2007
Paul;

I will try this, although I am somewhat unsure of the next step. Should I save this data in a Visual Basic or Java file? And then place that into the Photoshop plug-in folder? Please advise. Thanks a bunch. --Gordon
Paul Riggott
Inspiring
September 8, 2007
Is this something like you want?
Please make sure you back up first as this script DELETES the original before saving!
Please try on test first!

Paul.

var imageFolder = Folder.selectDialog("Select the folder with JPGs to process");
if (imageFolder != null) processFolder(imageFolder);

function processFolder(folder) {
var fileList = folder.getFiles()
for (var i = 0; i < fileList.length; i++) {
var file = fileList;
if (file instanceof File && file.name.match(/\.jpg$/i)) {
open(file);
var doc = app.activeDocument;
var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
var saveFile = new File(decodeURI(activeDocument.fullName.fsName));
//Any other processing commands ...
saveFile.remove();
SaveForWeb(saveFile,60);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;

} else
if (file instanceof Folder) {
processFolder(file);
}
}
}
function SaveForWeb(saveFile,jpegQuality) {
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);
}