On Jan 18, 2010, at 2:31 PM, Luther Fuller wrote:
I've never written a script object and I'm not familiar with "data and methods", so I'm not sure what he's getting at, but he is beginning to make some sense.
Every script is a script object. When you write a script, it is as if the entire script is implicitly surrounded with a “script … end script” statement.
So, when you write a script like
property MyName : "Script One"
say "My name is " & MyName it is almost as if you wrote
script property MyName : "Script One" on run say "My name is " & MyName end run end script
I say “almost” because if you run the above it creates a script object and sets that to the result, but it does not run the new script. To run the resulting script, you’d have to add
run the result
which is essentially what happens on your behalf when you run a script with Script Editor or osascript, or by opening a script applet.
Script objects are collections of properties and handlers. (Cocoa calls these “instance variables”—“ivars” for short—and “methods”, respectively.) They can also inherit properties and handlers from other script objects, via the “parent” property, which aids in script organization and enables the sharing of properties and the reuse of handlers.
The reasons for defining script objects are similar to the reasons for defining handlers. They help organize large or complex scripts into smaller pieces that are easier to work with, and they enable you to reuse code/objects.
Simple scripts just use the implicit “run” handler, more complex scripts may define other handlers. Similarly, simple scripts just use the single, “top-level” script object for their properties and handlers, more complex ones may define other script objects.
Here’s an example of using multiple instances (copies) of a script object to represent several different people. When you “copy” a script object, you create a new object whose properties can be changed without altering the original.
script Person property FullName : "Anonymous" property Title : "Unknown" on SayHello() set message to "Hello, my name is " & FullName & ", " & Title & "." log message say message end SayHello end script
copy Person to Elmer set Elmer's FullName to "Elmer J. Fudd" set Elmer's Title to "Millionaire"
copy Person to Shake set Shake's FullName to "Shake Zula" set Shake's Title to "Mike Rula"
tell Shake to SayHello() tell Person to SayHello() tell Elmer to SayHello() -- Chris Page
The other, other AppleScript Chris
|