Sunday, August 05, 2007

 

Active Document Gotcha.

If you have two documents open with the same name, InDesign scripting gets confused about which is which when you try to use app.activeDocument. For example, I had two documents called sampledocument.indd open at the same time this morning. One was being accessed over the network on a remote volume on my G5 while the other was local on my desktop. [I was doing this for testing purposes only--I don't think I've ever actually needed to have two documents open at the same time with the same name.]

For example, when I had the desktop version of my document at front, this script:
myDoc = app.documents[0];
$.writeln(myDoc.fullName);
app.activeDocument = app.documents[-1];
$.writeln(myDoc.fullName);
displayed this:
~/Desktop/sampledocument.indd
/davesaundersG5/Documents/Work/TestFolder/sampledocument.indd
and, when I ran it a second time, it returned:
/davesaundersG5/Documents/Work/TestFolder/sampledocument.indd
~/Desktop/sampledocument.indd
These are right both times. But this script:
myDoc = app.activeDocument;
$.writeln(myDoc.fullName);
app.activeDocument = app.documents[-1];
$.writeln(myDoc.fullName);
returned:
/davesaundersG5/Documents/Work/TestFolder/sampledocument.indd
/davesaundersG5/Documents/Work/TestFolder/sampledocument.indd
no matter which of the two documents I had at the front.

Perhaps the root of the issue is that this:
$.writeln(app.activeDocument.toSource());
returns:
resolve("/document[@name=\"sampledocument.indd\"]")
which contains an inadequate specifier for the document because the two names are the same. Perhaps this indicates that somewhere the documents are maintained is some fixed order so that the specifier always finds the same one no matter where it might be in the collection of documents.

The moral to the story is: never use activeDocument if you have two documents with the same name open at once.

Comments: Post a Comment

<< Home

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