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:
Perhaps the root of the issue is that this:
The moral to the story is: never use activeDocument if you have two documents with the same name open at once.
For example, when I had the desktop version of my document at front, this script:
myDoc = app.documents[0];displayed this:
$.writeln(myDoc.fullName);
app.activeDocument = app.documents[-1];
$.writeln(myDoc.fullName);
~/Desktop/sampledocument.inddand, when I ran it a second time, it returned:
/davesaundersG5/Documents/Work/TestFolder/sampledocument.indd
/davesaundersG5/Documents/Work/TestFolder/sampledocument.inddThese are right both times. But this script:
~/Desktop/sampledocument.indd
myDoc = app.activeDocument;returned:
$.writeln(myDoc.fullName);
app.activeDocument = app.documents[-1];
$.writeln(myDoc.fullName);
/davesaundersG5/Documents/Work/TestFolder/sampledocument.inddno matter which of the two documents I had at the front.
/davesaundersG5/Documents/Work/TestFolder/sampledocument.indd
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.