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?
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.
//DESCRIPTION: Open a copy of a documentThis gets the job done. It's a little more convoluted than just selecting an option. Perhaps some explanation is in order.
openCopy();
function openCopy() {
var numDocs = app.documents.length;
if (File.fs == "Windows") {
var Filter = "InDesign documents: *.indd";
} else {
var inddFilter = function(file) {
while(file.alias){
file = file.resolve();
if (file == null) return false;
}
if (file instanceof Folder) return true;
return (file.name.slice(file.name.lastIndexOf(".")).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 = app.open(myFile, 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.");
return
}
var name = "temp";
var inc = 0;
do {
var myTemplate = File("~/Desktop/" + name + inc + ".indt");
inc++;
} while (myTemplate.exists);
myDoc.save(myTemplate, true); // save as template
app.documents[0].close();
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
app.open(myTemplate);
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
myTemplate.remove();
}
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.