简体   繁体   English

如何使用脚本关闭 Illustrator 中所有打开的文档?

[英]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.我正在尝试编写一个脚本,该脚本可以关闭 illustrator 中所有打开的文档,并提示我是否要保存它。 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):可能它对您有用(成功取决于 function generateText()的连接方式):

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.但是如果你出于某种原因想使用document.activate()方法,可能会有麻烦。 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).据我所知,Illustrator 会在您向后迭代集合时更改集合app.documents中已激活文档的索引或类似的东西(从结尾到开头,如果您想关闭文档并更改,这是必要的分别收藏)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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