21.3. Loading Scripting AdditionsA scripting addition is not targeted; it is loaded. In particular, in order to be seen and used by a script, a scripting addition must be loaded by the AppleScript scripting component instance that is going to compile or run that script (see Chapter 3). For that to happen, the scripting addition file must be physically installed in any of a specific set of locations at the time that the AppleScript component instance is summoned into existence. On Mac OS X, those locations are, in the first instance, the three /Library/ScriptAdditions directoriesin /System, at the top level, and in the user directory. (On previous systems, there was one location, the Scripting Additions folder; this was originally in the Extensions folder but was moved into the System Folder starting in Mac OS 8.) This architecture has historically caused headaches for script developers. If you wanted to write a script relying on a third-party scripting addition and distribute that script to others, you had to worry about how to guarantee that your end user had the right scripting addition in the right location by the time the script ran. Typically this involved social engineering. First you had to ascertain what scripting additions your script was calling. (Script Debugger is especially helpful here; it lists the scripting additions on which your script depends, even looking up any unresolved event codes on http://macscripter.net for you.) Then you had to distribute the required scripting additions with your script, or otherwise include instructions for acquiring them, along with loud and clear instructions on installing the scripting additions, in the hope of staving off complaints when your end user heedlessly tried to use the script without installing the scripting additions and got an error. Starting in Panther, an elegant solution to this longstanding difficulty was implemented at last: if you are willing to distribute your script as an applicationeither as an applet bundle or as an AppleScript Studio applicationthen if that bundle contains a directory Contents/Resources/Scripting Additions, any osaxen in that directory will be loaded when the application starts up. The needed osaxen must, however, be installed in one of the standard locations on your machine ("you" being the developer) as otherwise the script cannot be compiled in the first place. And of course the effectiveness of this solution is limited to systems that implement the mechanism involved, which means Panther and later. (On what an applet bundle is, see "Applet and Droplet" in Chapter 3. For examples, see Chapter 27.)
There is one other trick for making a scripting addition available on an end user's machine, and that is for your script to install it into a ScriptingAdditions folder. You might think this couldn't possibly work, because by now the script is running, the AppleScript scripting component instance has already been created, and any scripting additions that are going to be loaded have already been loaded. You're right, but there's a magic spell for telling the AppleScript component to reload osaxen: try tell me to «event ascrgdut» end try The Apple event must be issued in raw form, as it has no English-like equivalent (it's not in any dictionary). It's enclosed in a try block because it will probably generate an error; but the error is spurious and may be safely ignored. Thus, your script can install a scripting addition on the fly and call a command within it. Here's an example using the Jon's Commands osax. We will call the ticks , a command within Jon's Commands; we use run script so that the osax doesn't have to be installed in order to compile and test the script (alternatively, we could use the command's four-letter code, «event JonstikC»). To install Jon's Commands, we have to find a copy of it; here, we ask the user to locate it:
try
run script "get the ticks"
on error -- evidently it isn't installed
set jons to choose file with prompt "Please find Jon's Commands:"
set sa to path to scripting additions from user domain
tell application "Finder" to duplicate jons to sa
try
tell me to «event ascrgdut»
end try
end try
display dialog (run script "get the ticks") -- 1974834
|