添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I've written a simple script to open two documents in Adobe Illustrator (File -> Scripts) and apply some commands to those documents.

The purpose of the script; with no documents open in Illustrator, the script will:

  • Open the required documents.
  • Load an XML variable library into those documents.
  • Display the dataset in that library (there is only one dataset).
  • Save the file in a given location.
  • Close the file.
  • The script:

    var doc_1 = open(File("C:/file1.ai")); var doc_2 = open(File("C:/file2.ai")); var sourceDoc; var targetFile; var options = new IllustratorSaveOptions(); for (var i = 0; i < app.documents.length; i++ ) sourceDoc = app.documents[i]; sourceDoc.importVariables(new File("C:/variables.xml")); sourceDoc.dataSets[0].display(); targetFile = new File("C:/output" + sourceDoc.name + '.ai'); sourceDoc.saveAs( targetFile, options ); sourceDoc.close(); catch(err) alert(err);

    This code works only for the last opened file (file2.ai). file1.ai is never touched, almost as if there is no loop.

    What could be causing this?

    When closing the first file in the loop you just moved app.documents[1] to app.documents[0]

    You want top loop through them backwards

    try your loop with

    for (var i = app.documents.length; i >0 ; i-- ) 
                    This is the answer. I noted this same behavior when saving the files one by one out of a loop as well. You have to save them in reverse order.
    – George 
                    Jun 5, 2018 at 7:55
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid …

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.