skip to Main Content

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

}
}

2

Answers


  1. 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?

    Login or Signup to reply.
  2. 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.
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search