Saturday, August 27, 2005
Everything's an Object
One of the powers of JavaScript is that just about anything you work with is either an object or a property of an object. What’s more, some properties can themselves be objects too, so you end up with a hierarchy of objects. To take a simple example, consider the File System. Here, the objects are the files and folders of your file system.
Once I realized this, it opened my eyes to a new world of possibilities. For example, it allowed me to create a new method for my strings that mimics the AppleScript construct is in. In AppleScript (which I used to script InDesign 1.5 and 2.0 before JavaScript became available), a very useful feature was the ability to ask if a string was in an array of strings using something like:
Having grasped this, I created the following definition of a new method for strings in my script:
It wasn't long before I realized that this method could be generalized to work with any object by simply changing the declaration to:
Well, even though it's Saturday morning, I have work I must attend to, so that's it for now.
By The Way: Objects vs. Object Model
- It takes some getting used to, separating the concept of an object model from the actual objects you work with. An object model defines how the objects of a "universe" relate to each other and what their properties are. An object is a particular object within the specific instance of its object model that you happen to be working with.
For example, the InDesign object model says that a property of the application object is that it has a collection of documents. But, if you are not running InDesign, then you don't have an actual application object for your scripts to operate upon. Or, if you have no documents open, then the collection of documents for your InDesign at this moment is empty.
Once I realized this, it opened my eyes to a new world of possibilities. For example, it allowed me to create a new method for my strings that mimics the AppleScript construct is in. In AppleScript (which I used to script InDesign 1.5 and 2.0 before JavaScript became available), a very useful feature was the ability to ask if a string was in an array of strings using something like:
- If myString is in myString Array then ...
Having grasped this, I created the following definition of a new method for strings in my script:
String.prototype.isInArray = function(myArray){Armed with this, I could write statements like:
for (var i=0; myArray.length > i; i++) {
if(myArray[i] == this){
return true;
}
}
return false;
}
If (myStyleName.isInArray(myDoc.paragraphStyles.everyItem()) {This code snippet tests to see if the document referenced by the variable I named "myDoc" includes a paragraph style with the name contained in the variable I named "myStyleName".
It wasn't long before I realized that this method could be generalized to work with any object by simply changing the declaration to:
Object.prototype.isInArray = function(myArray){While it is rare that I use this method with anything other than a string, the possibility is always there.
Well, even though it's Saturday morning, I have work I must attend to, so that's it for now.
Comments:
<< Home
I suspect that my AppleScript syntax isn't quite right. It's amazing how quickly details fade from memory. Scripting is definitely not like riding a bike!
Post a Comment
<< Home