Sunday, January 14, 2007
Object Specifiers
If you do a search of the JavaScript section of the InDesign CS2 Scripting Reference for the words "object specifier" you'll get 448 hits. Half of them are found in the descriptions of the getElements() method. The other half are in the descriptions of the toSpecifier() method.
So, what are these object specifiers? I confess to having some difficulty explaining what they are in abstract terms. I think of them as dynamic pointers into the object structure of InDesign.
Which begs the question: what is the object structure when it is at home? Isn't the term "object model"? Well, yes, most people speak of the object model all the time, but I see a difference between the model: all the possible ways that objects might related to each other, and the structure: the current set of objects and their interrelationships. The model describes what might happen at some point; the structure reflects what is happening now.
For example, if you have no documents open, the model still includes pageItems, but with no documents for those objects to exist in, the structure includes no pageItems right now. Of course, as you work with InDesign, whether with scripts or with the UI, the structure changes to reflect your actions.
So, an object specifier points at an object in the structure. Well, maybe. It's a little more complicated than that because it might point at an object that doesn't yet exist. Look at this:
Let's take a look at that toSpecifier() method:
If we modify this last script to read:
Let's take this one step further:
I'll have more to say on this subject as I explore further.
So, what are these object specifiers? I confess to having some difficulty explaining what they are in abstract terms. I think of them as dynamic pointers into the object structure of InDesign.
Which begs the question: what is the object structure when it is at home? Isn't the term "object model"? Well, yes, most people speak of the object model all the time, but I see a difference between the model: all the possible ways that objects might related to each other, and the structure: the current set of objects and their interrelationships. The model describes what might happen at some point; the structure reflects what is happening now.
For example, if you have no documents open, the model still includes pageItems, but with no documents for those objects to exist in, the structure includes no pageItems right now. Of course, as you work with InDesign, whether with scripts or with the UI, the structure changes to reflect your actions.
So, an object specifier points at an object in the structure. Well, maybe. It's a little more complicated than that because it might point at an object that doesn't yet exist. Look at this:
myObjSpecifier = app.documents.item("Fred.indd");Assuming you don't have a document named "Fred.indd" open when you run that script, you'll get the alert. Compare that with this:
if (myObjSpecifier == null) {
alert("'Fred.indd' doesn't exist, yet.");
}
myObjSpecifier = app.documents.item("Fred.indd");Run this script, and even though we set the value of myObjSpecifier before the document was created, you get the message that Fred.indd now exists. This is why I call this a dynamic pointer into the object structure. Its value adapts to the moment.
var myFolder= Folder.selectDialog("Choose a home for Fred.indd");
if (myFolder == null) {exit()}
app.documents.add();
app.documents[0].save(File(myFolder.absoluteURI + "/Fred.indd"));
if (myObjSpecifier == null) {
alert("'Fred.indd' still doesn't exist");
} else {
alert("'Fred.indd' exists now.");
}
Let's take a look at that toSpecifier() method:
myObjSpecifier = app.documents.item("Fred.indd");Run this in ESTK and you see in the console:
$.writeln(myObjSpecifier.toSpecifier());
/document[@name="Fred.indd"]We can ignore the second line; that's just the result of the script. What's interesting is the first line. That's what an object specifier looks like when it is coerced to text. As you can see, it is a descriptive pointer into the object structure.
undefined
If we modify this last script to read:
myObjSpecifier = app.documents.item("Fred.indd");And again run it from ESTK, this time, we see in the Console:
$.writeln(myObjSpecifier.toSpecifier());
myObjSpecifier
/document[@name="Fred.indd"]This time, the second line is of interest. The result of our script is an object of class Document. That it doesn't exist (yet) is irrelevant (as far as the ExtendScript engine is concerned).
[object Document]
Let's take this one step further:
myObjSpecifier = app.documents.item("Fred.indd");Hmmm, the result of running this is an eye-opening disappointment. To execute the second statement, the engine has to evaluate the variable myObjSpecifier. And, in this case, because we're running with no document named "Fred.indd", it evaluates to null. Apparently, when that happens, it does more than just evaluate the variable; it resolves it, so it becomes undefined, causing the third line of the script to give the error:
myObjSpecifier == null;
$.writeln(myObjSpecifier.toSpecifier());
Object is invalidThis leaves me scratching my head a little, making the ability to pre-create object specifiers more than a little dodgy if they are this fragile.
I'll have more to say on this subject as I explore further.
Comments:
<< Home
Hi
through VBScript we can work on indesign Objects from any place(i.e no need to place your script in Preset>>Script Folder)
beacuse you can use CreateObject("Indesign.Application.CS2")
but through JavaScript we can not do this (ActiveXObject is not working)
how we can access "app" object from outside the Script Folder
or how we reference "app" from any other location ??
please help
through VBScript we can work on indesign Objects from any place(i.e no need to place your script in Preset>>Script Folder)
beacuse you can use CreateObject("Indesign.Application.CS2")
but through JavaScript we can not do this (ActiveXObject is not working)
how we can access "app" object from outside the Script Folder
or how we reference "app" from any other location ??
please help
how we can access "app" object from outside the Script Folder
or how we reference "app" from any other location ??
Post a Comment
or how we reference "app" from any other location ??
<< Home