Sunday, August 26, 2018
Why not just use app activeDocument
Why not just use app activeDocument
The need for document references.
In recent days Ive posted two different generic ExtendScript functions for getting a document reference from an object.
Why can we just use:
var docRef = app.activeDocument ;
Simple: It doesnt always work. .activeDocument is the application property for the active document -- the document the user is using. However, when using Adobe InDesign Server, there is never an active document and thus it fails. So, without anything else going on, app.activeDocument doesnt always work, and if you cant count on it even part of the time, you should use it.
Well, if I cant count on it all the time I wont use it. Many of the ExtendScript scripts I create for clients are clients that I have never met, sometimes never even spoken too on the telephone. I need the scripts to work 100% of the time -- dont you?
Well then, why not use:
app.documents[0] ;
That gives you the front document, which should be the one you are using. Yes, it generally does. But for that to work, you need to have the document open in a visible window. And one of the the options for opening a file is to open it without displaying it. And if you dont display it, it isnt in front. And if it isnt in front, it wont be document[0]. Thus, it too doesnt work 100% of the time. Do you want your automobile to only be drivable some of the days? Perhaps that is a bad analogy -- you might actually like it if you couldnt drive to work some days.
There is another problem with both of these functions, lets assume you have a group of Adobe InDesign documents open. Lets assume you are using a findObject search function. Lets assume you locate an object on one of your open documents. Which document is it? It likely isnt the .activeDocument or .documents[0], so you need a function like the recently posted generic functions to point you to the particular document.
Another case where these two application properties wont work; Lets assume you are constructing a new document from an old document -- for example I have a script I wrote for a client that replicates an ad stack from a converted QuarkXPress document (I think the client produced it with BrainWorks) to an Adobe InDesign document created from their current template. In this case we will always have two documents open and will need to have good static references to both of them. The documents dont have to be switching between active and not (just because you cant see a document doesnt mean that you cant be manipulating it) but even if both are visible, only one will ever be the .activeDocument. You can manipulate things in a document that isnt the active document or the first document.
Good document references allow your scripts to work reliably. And with automation, reliability is more important than raw speed.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.