Monday, August 20, 2007


Open a Copy

In the UI, you get the choice to open a copy of a document in the File > Open dialog. This option is missing from scripting, so how to achieve the same goal?
//DESCRIPTION: Open a copy of a document
function openCopy() {
  var numDocs = app.documents.length;
  if (File.fs == "Windows") {
    var Filter = "InDesign documents: *.indd";
  } else {
    var inddFilter = function(file) {
        file = file.resolve();
        if (file == null) return false;
      if (file instanceof Folder) return true;
      return (".")).toLowerCase() == ".indd");
    var Filter = inddFilter
  var myFile = File.openDialog("Choose an InDesign document", Filter);
  if (myFile == null) { return } // user canceled
  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
  var myDoc =, false);
  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
  if (numDocs == app.documents.length) {
    alert("Unable to open document; perhaps it is already open by you or another user.");
  var name = "temp";
  var inc = 0;
  do {
    var myTemplate = File("~/Desktop/" + name + inc + ".indt");
  } while (myTemplate.exists);, true); // save as template
  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;;
  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
This gets the job done. It's a little more convoluted than just selecting an option. Perhaps some explanation is in order.

The numDocs variable is used to make sure we actually succeed in opening the selected document. The first time I tested the script, I happened to choose the document I already had open. But I got no warning about this because I had switched off interaction. I ended up with an untitled copy of the document, but that was more luck than judgment.

The next few lines show how easy Windows users have when it comes to filtering file types. While they get to use a simple string, Macintosh users get to write a function that not only has to deal with the files in question but also folders and aliases. As a result, we're halfway through the script before we even ask the user to locate the file.

Notice that I open the file without a window. There's no point in letting the user see this step--it just causes unpleasant flashing of the screen. We need somewhere to save the document as a temporary template so we can then open that and get our copy. The desktop seems the obvious place. But we need to make sure that the file name we choose isn't already in use by an existing file. That's what the do/while loop is all about.

Once we have a unique name, all we have to do is save as a template, then open the template (this time with a visible window), and having opened it, we delete it so it doesn't clutter up the user's desktop.

And we have achieved our goal: we've opened a copy of the document.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?