user1825922
user1825922

Reputation: 35

close all open documents after saving

I have the script partially working. It saves all the open psd as jpg into into a separate directory and it close some of the open files not all. The directory has five files. The script saves only three files, What am I doing wrong?

#target photoshop

if (app.documents.length > 0) {
//flatten the active document
app.activeDocument.flatten(); 

//jpeg options
var myJPEGOptions = new JPEGSaveOptions();
myJPEGOptions.embedColorProfile = true;
myJPEGOptions.formatOptions = FormatOptions.STANDARDBASELINE;
myJPEGOptions.matte = MatteType.WHITE;
myJPEGOptions.quality = 12;
myJPEGOptions.scans = 3;

// get documents;
var docs = app.documents;
for (var m = 0; m < app.documents.length; m++) {
app.activeDocument = docs[m];

try {
//save file to folder
var myFile = new File(("~/Desktop/forum-test") + "/" + activeDocument.name); 
app.activeDocument.saveAs(myFile, myJPEGOptions, true); 

//close the document
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}


catch (e) {
alert ("Error the script did not execute");
}

}
}

Upvotes: 0

Views: 3068

Answers (2)

Bj&#248;rn van Dommelen
Bj&#248;rn van Dommelen

Reputation: 1097

The app.documents collection is dynamic so when you close a document this collection is changed accordingly.

Because you are closing your document inside a for loop where you compare an incrementing index against app.documents.length you are not processing all files (as app.documents.length decreases by one each time the loop is processed).

Try a while loop instead:

while (app.documents.length){
  // Save and close the active document here.
}

Upvotes: 2

acontell
acontell

Reputation: 6932

I think this two lines are wrong:

//save file to folder
var myFile = new File(("~/Desktop/forum-test") + "/" + activeDocument.name);

and

//close the document
activeDocument.close(SaveOptions.DONOTSAVECHANGES);

Shouldn't you be using app.activeDocument instead of activeDocument? What is activeDocument anyway?

Upvotes: 0

Related Questions