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

Paul Riggott
Inspiring
January 11, 2008
The files must be very large.
On my machine
AMD Athlon 64 3200+ 2.01GHz 1GB Ram
It failed with an uncompressed file 347.6MB (40.9MB on Hard Disc) with not enough memory.
It managed to do a 277.4MB uncompressed file (34.3MB) with no problem although it was slow.
With such large files it might be betted to just do a folder at a time so there are two new vars that you can alter.
MAXJPGSIZE this is the size of the file on the Hard Drive.
SUBFOLDERS setting this to true will include subfolders and false will only process the selected folder.
Paul.



///////////////////////////////////////////

//GLOBAL vars, amend values to suit.

//Maximum size of file in Megabytes to process (This is the file size on the Hard Drive)

MAXJPGSIZE = 34;

// "true" = include subfolders - "false" do not include subfolders

SUBFOLDERS = true;

////////////////////////////////////////////

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)) {

if((file.length/1024/1024).toFixed(0) < MAXJPGSIZE +1){

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));



saveFile.remove();

SaveForWeb(saveFile,60);

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

app.purge(PurgeTarget.ALLCACHES);

$.gc();

app.preferences.rulerUnits = strtRulerUnits;

app.preferences.typeUnits = strtTypeUnits;

}

} else

if (file instanceof Folder && SUBFOLDERS) {

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);

}

Participating Frequently
January 11, 2008
I found a bigger issue with the script. The Save for Web functionality does not work with large images. When you try and save for web by hand it gives a message saying that the image is too large and might end up giving you out of memory errors, and the do...

Does anyone know what the size limit is?
Is it file size or pixels?

Could we put a if around the convert that makes it so it only converts files within the limits?

I ended up losing a bunch of images because of this, because I could not use them and when I tried to re-save them normally the quality was terrible.

I am wondering if the eating up memory issue is related.
Paul Riggott
Inspiring
January 10, 2008
The only other thing would be to purge the cahes and do a garbage collection:-





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));

saveFile.remove();

SaveForWeb(saveFile,60);

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

app.purge(PurgeTarget.ALLCACHES);

$.gc();

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);

}

Participating Frequently
January 10, 2008
So I tried this again, and checked my Trash folder and it is not putting them in the Trash.

So I have no clue what is going on...
Paul Riggott
Inspiring
January 9, 2008
I haven't tried it on a large number of files or on a Mac.
Just a thought, as the script DELETES the original then writes the new file. What happens to the deleted files on a Mac, do they get put in a trash bin? If so that might account for the space issue.
Participating Frequently
January 9, 2008
I tried running this script first on a small folder and it worked great, however when I tried running it on a folder with a lot of items it started eating up disk space like crazy. Within 5 minutes I was down over 5Gig of disk space. I finally had to kill Photoshop because I was out of space, and to regain the space I had to restart my machine.

Is this normal?
I didn't think it would do that because it replaces the original file.
Is there a way to get it not to eat up space?

Running Mac OSX Leopard

Thanks!
Participant
September 12, 2007
Paul;

I revised the script using your recommendation, and it works fine now, as intended. Thanks for your help. Brilliant!
-- Gordon
Paul Riggott
Inspiring
September 11, 2007
Thanks Micheal I tried using the PRE tags in another Message but still had problems, I must remove all TABs as well then maybe it will be readable?
Paul
Inspiring
September 11, 2007
You can use the PRE tag here to avoid the forum reformatting your code.

Mike
Paul Riggott
Inspiring
September 11, 2007
Thanks Larry, unfortunatly the script was written for InDesign, I don't have that and it doesn't run in Photoshop. BUT at least it gives the basics of whats required.
Many thanks.
Paul.