Simple script to open and save file

Home Forums InDesign Add-ons (Scripts, Scripting, and Plug-ins) Simple script to open and save file

Tagged: , , ,

This topic contains 4 replies, has 3 voices, and was last updated by  Mike Dean 1 week, 2 days ago.

  • Author
  • #98821

    Morgan Waage

    I am trying to learn some InDesign scripting with Javascript.
    Eventually I want to build something larger but for now I am just looking for the codes to:
    open document called /Users/morgan/Desktop/test.indt
    save document at /Users/morgan/Desktop/test01.indd
    close document

    I can’t find any examples of something so simple online. Maybe because it’s not very useful as a script. But for me to start learning it would help a lot.

  • #98827

    Mike Dean

    Hi Morgan,

    It’s pretty simple but you have to create a new file object for both the template you’re opening and file you’re saving before you can open/save. Example code is below. Hope this helps, and good luck!

    //create a new file object for the template
    var templateFile = new File("~/Desktop/test.indt"); //where ~/Desktop is a username-independent path to the desktop
    //open template
    var template =;

    //create a new file object for the test01 file
    var docFile = new File("~/Desktop/test01.indd");
    //save as. NOTE: This will overwrite without asking, so be careful
    var doc =

    //do stuff in file here

    //close and save doc file

  • #98836

    Peter Kahrel

    Hi Mike — Actually, there’s no need to create new file objects separately. You can do it as follows:

    var template = (File ('~/Desktop/test.indt'));
    // Do stuff (File('~/Desktop/test01.indd'));


  • #98847

    Morgan Waage

    Excellent! Thanks guys.
    Should I put this inside a try catch block or isn’t that necessary?

  • #98859

    Mike Dean

    Ah, even simpler. Thanks Peter!

    It would be a good idea to add some error checking in there, especially on the save file step. It currently overwrites without asking, and you probably don’t want that longer term. You could add a step to check for an existing file, and if it exists either ask the user if they want to overwrite. Or it could automatically create a backup copy of the existing file.

You must be logged in to reply to this topic.