views:

50

answers:

1

This following script almost does what I need. What I'm trying to do is go through the opened documents, 139 of them, and save them as jpeg. However what it's lacking is moving from one opened document to the other, so it saved the same image over 139 times. I assumed doc.close() would close the opened document and give a new one focus, but it doesn't.

Here's the code:

var destination = "C:/Documents and Settings/Administrator/My Documents/small images"
for(var i = 0; i < 5; i++)
{
    doc = documents[i];
    name_ = doc.name.substring(0, doc.name.indexOf('.'))
    saveForWebPNG(destination, name_);
    doc.close();
}

function saveForWebPNG(outputFolderStr, filename)
{
    var opts, file;
    opts = new ExportOptionsSaveForWeb();
    opts.format = SaveDocumentType.JPEG;
    opts.quality = 60;
    if (filename.length > 27) {
        file = new File(outputFolderStr + "/temp.jpg");
        activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts);
        file.rename(filename + ".jpg");
    }
    else {
        file = new File(outputFolderStr + "/" + filename + ".jpg");
        activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts);
    }
}
+1  A: 

According to the Adobe Photoshop CS2 JavaScript Scripting Guide it looks like you need to assign to the Application.activeDocument property to make that document the currently selected one for any actions. This makes sense since you're using that property in the saveForWebPNG function without explicitly activating the document in the iterator in the first block. It might be as simple as the following change:

for (var i = 0; i < 5; i++) {
  var doc = documents[i];
  app.activeDocument = doc; // Select that document.
  var name = doc.name.substring(0, doc.name.indexOf('.'))
  saveForWebPNG(destination, name);
  doc.close();
}

However, I don't have a copy of Photoshop and haven't verified this solution.

maerics