DekGenius.com
[ Team LiB ] Previous Section Next Section

20.2 Scripting Additions and Speed

One of the main reasons for using scripting additions is speed. For repeated trigonometric calculations, for example, it is certainly going to be a lot faster to use a scripting addition, such as the Satimage osax, than to roll your own calculation, as disingenuously suggested at Apple's web site. Similarly, a scripting addition that implements transformations to a list, such as returning a list with a particular element deleted, is going to be faster than coding the same operation in AppleScript (see Section 5.5).

Just how quickly a scripting addition is called, though, depends upon how you call it. The osax architecture is such that a scripting addition appears to be present "inside" whatever application is being targeted when the scripting addition command is called. This is noticeable, and useful, when a scripting addition puts up some user interface. For example, if the display dialog command is called from within a tell block targeting the Finder, the dialog appears within the Finder; it's as if you'd given the Finder a new dialog.

Behind the scenes, the way this works is that the application is sent the Apple event denoting a scripting addition command and can't deal with it; the message is then sent on up to the realm of scripting additions as a kind of fallback. This means that when you use a scripting addition command while targeting an application, it must go through an extra step. If you use a scripting addition command outside of any tell block, or within a "tell me" block, the message is sent directly to the scripting addition, which is faster by about an order of magnitude:

set t to the ticks
repeat 5000 times
        tell application "Finder" to get offset of "i" in "ticks"
end repeat
set t1 to (the ticks) - t
set t to the ticks
repeat 5000 times
        tell me to get offset of "i" in "ticks"
end repeat
set t2 to (the ticks) - t
return {t1, t2} -- {944, 71}

(The command the ticks comes from the Jon's Commands osax, and is good for timing things; a tick is about one-sixtieth of a second.)

    [ Team LiB ] Previous Section Next Section