Question

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);
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

I know this is a lot late, but I just came across this question and feel like I might have a solution that someone down the road may be able to use. So here goes.

One solution is to adjust the for loop. Let's say there are 5 documents open. app.documents.length would be 5. When you close one, the length is now 4, then 3, etc. i is counting up as app.documents.length is decreasing. When i = 3, app.documents.length = 2. I think that iterating backwards would do the trick.

for (i = app.documents.length - 1; i >= 0; i--) { ... }

+1 for jbiz's solution. I couldn't figure out why my loop wouldn't complete.

for (i=app.documents.length-1; i >= 0; i--) {
  doc = app.documents[i];
  app.activeDocument = doc;

  ...
  do whatever you need, save & close
  ...
}

app.documents.length gets shorter with every iteration though the loop if you close the document when you're done.

You want to close the current selected (or active) document:

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top