简体   繁体   中英

How can I close all open documents in illustrator with script?

I am trying to write a script that can close all open document in illustrator and prompt me if I want to save this or not. This has to be done with script because my full script is about getting a job done and closing all open docs after it. But no matter what I try it always gives me some error. Can anyone give me a possible solution for it?

This is my code:

for(var i = 0; i < app.documents.length; i++ ){
    app.documents[i].activate();
    generateText(); //this is the function that needs to be execute first
    app.activeDocument.close(SaveOptions.PROMPTTOSAVECHANGES); //not work properly
    //app.executeMenuCommand("close"); //does not work either
    }

Probably it will work for you (the success depends on how the function generateText() is wired):

while (app.documents.length) {
  generateText();
  app.activeDocument.close();
}

It will ask to save a document only if this document has some unsaved changes.

But if you want to use document.activate() method by some reason there can be troubles. As far as I can tell Illustrator changes indexes of activated documents in the collection app.documents or something like that when you're iterating the collection backward (from the end to the start, which is necessary if you want to close the documents and change the collection respectively).

So here is a workaround:

// create an array of documents instead of the native collection
var docs = [];
var i = app.documents.length;
while (i--) docs.push(app.documents[i]);

// iterate over the array
var i = docs.length;
while(i--) {
    docs[i].activate();
    generateText();
    docs[i].close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM