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 6 replies, has 4 voices, and was last updated by  Peter Kahrel 1 month, 2 weeks ago.

  • Author
    Posts
  • #98821

    Morgan Waage
    Member

    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
    Member

    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 = app.open(templateFile);

    //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 = template.save(docFile)

    //do stuff in file here

    //close and save doc file
    doc.close(SaveOptions.YES)

  • #98836

    Peter Kahrel
    Member

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

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

    Peter

  • #98847

    Morgan Waage
    Member

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

  • #98859

    Mike Dean
    Member

    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.

  • #99233

    Ian Peters
    Member

    Newbie here too, so sorry if it’s a stupid question, but…

    If the file opening is a .indt file and it’s saving as an .indd, I would guess you wouldn’t replace the template with an indesign file as they have differing extensions.
    So wouldn’t you end up with a template file and a new indesign file by running this script? Or am I incorrect and it’s saving over somehow.

  • #99234

    Peter Kahrel
    Member

    > I would guess you wouldn’t replace the template with an indesign file as they have differing extensions.

    Correct.

    > So wouldn’t you end up with a template file and a new indesign file by running this script?

    Correct.

You must be logged in to reply to this topic.