Simple script to open and save file
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Simple script to open and save file
- This topic has 7 replies, 5 voices, and was last updated 4 years ago by Monisha Rajendran.
-
AuthorPosts
-
-
October 12, 2017 at 12:53 am #98821Morgan WaageMember
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 documentI 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.
-
October 12, 2017 at 5:32 am #98827Mike DeanMember
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) -
October 12, 2017 at 12:00 pm #98836Peter KahrelParticipant
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
-
October 13, 2017 at 4:06 am #98847Morgan WaageMember
Excellent! Thanks guys.
Should I put this inside a try catch block or isn’t that necessary? -
October 13, 2017 at 11:36 am #98859Mike DeanMember
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.
-
October 26, 2017 at 5:04 am #99233Ian PetersMember
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. -
October 26, 2017 at 5:32 am #99234Peter KahrelParticipant
> 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.
-
March 2, 2020 at 12:55 am #12343125Monisha RajendranMember
Hi everyone,
I need a script to preview the document before the document gets open after previewing the whole document the user can restore the particular document…???? Please provide me some suggestion for getting the contents of the document…Please do guide on this………
-
-
AuthorPosts
- You must be logged in to reply to this topic.