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

Participant
June 7, 2008
Hi Thanks,

but it still not clear to me.

So What should I do now?
Inspiring
June 6, 2008
It should work in CS2.

Most of the time when I see that error message it's because I am running the script in ESTK and forgot to set the target app to Photoshop.
Participant
June 6, 2008
I strictly script Photoshop using Applescript, but I believe the cause for this error is that export save for web wasn't exposed to the scripting API until CS3.
Participant
June 6, 2008
When I ran the script in CS2, I received an error message

Error 22: ExportOptionsSaveForWeb() does not have a constructor.

Being a JS-idiot, what does this mean and how do I fix this?

Thanks in advance
Paul Riggott
Inspiring
January 13, 2008
Sorry wouldn't know where to go next, maybe someone else has a solution?
Participating Frequently
January 12, 2008
Sorry for the on slot of messages...

So even with the resizing and with using:

app.purge(PurgeTarget.ALLCACHES);
$.gc();

It still eats up my disk space like crazy...
Running it on like 20 files ate 1/2 a gig.
Participating Frequently
January 12, 2008
what I did was add in an image resize for large images.

Here is what I came up with...

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

if (imageFolder != null) processFolder(imageFolder);

function cTID(s) { return app.charIDToTypeID(s); }
function sTID(s) { return app.stringIDToTypeID(s); }

function processFolder(folder)
{
Options = {};
Options.MAX_WIDTH = 640;
Options.MAX_HEIGHT = 480;

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

resizeImage(doc, Options);

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

function fitImage(doc, width, height) {
function _ftn() {
var desc = new ActionDescriptor();
desc.putUnitDouble( cTID('Wdth'), cTID('#Pxl'), width );
desc.putUnitDouble( cTID('Hght'), cTID('#Pxl'), height );

var fitId = sTID('3caa3434-cb67-11d1-bc43-0060b0a13dc4');
executeAction(fitId , desc, DialogModes.NO );
};

_ftn();
}

function resizeImage(doc, opts) {
var originalRez = doc.resolution;

if (doc.width.value > opts.MAX_WIDTH ||
doc.height.value > opts.MAX_HEIGHT) {
doc.resizeImage(undefined, undefined, 72, ResampleMethod.NONE);
fitImage(doc, opts.MAX_WIDTH, opts.MAX_HEIGHT);
doc.resizeImage(undefined, undefined, originalRez, ResampleMethod.NONE);
}
}
Participating Frequently
January 12, 2008
Here is the error. I just got it on a 36.4M image.

The image exceeds the size Save for Web was designed for. You may experience out of memory errors and slow performance. Are you sure you want to continue?

Yes I know it's a large file for the web, but thats not the point. The point is the script changes the file anyways and that wrecks the file.
Participating Frequently
January 12, 2008
hmm... I have 3G of memory and I was getting this on like 1.5M files.
I got the message when I did this by hand not when using the script.
The script would do it, and it would show on my computer fine.
It was when I tried to upload the image and view the image online that it wouldn't work.
Paul Riggott
Inspiring
January 11, 2008
Forgot to mention 1.5GB virtual memory and Photoshop CS3, allocted 539MB of actual memory.
This does not take into account other Photoshop settings ie: History states, cache levels etc.
So I suppose each machine would have a different Maximum file size and you would have to do a test to see what your Maximum is.