Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script Needed to work with "Save for Web" Tool

New Here ,
Sep 08, 2007 Sep 08, 2007
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?
TOPICS
Actions and scripting
3.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Sep 08, 2007 Sep 08, 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);
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 08, 2007 Sep 08, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 08, 2007 Sep 08, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 11, 2007 Sep 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 11, 2007 Sep 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 11, 2007 Sep 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 11, 2007 Sep 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 11, 2007 Sep 11, 2007
You can use the PRE tag here to avoid the forum reformatting your code.

Mike
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 11, 2007 Sep 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 12, 2007 Sep 12, 2007
Paul;

I revised the script using your recommendation, and it works fine now, as intended. Thanks for your help. Brilliant!
-- Gordon
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 09, 2008 Jan 09, 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!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 09, 2008 Jan 09, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 10, 2008 Jan 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...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 10, 2008 Jan 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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 10, 2008 Jan 10, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 11, 2008 Jan 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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 11, 2008 Jan 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 11, 2008 Jan 11, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 11, 2008 Jan 11, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 11, 2008 Jan 11, 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);
}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 11, 2008 Jan 11, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 13, 2008 Jan 13, 2008
Sorry wouldn't know where to go next, maybe someone else has a solution?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 05, 2008 Jun 05, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 06, 2008 Jun 06, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines