21.2. Classic Scripting Additions
There is a difference between a scripting addition intended to be used with Mac OS 9 or before and a scripting addition intended to be used with Mac OS X. A Mac OS X-type osax will not work on Mac OS 9. A Mac OS 9-type osax will work on Mac OS X only if it has been Carbonized, meaning that internally its Toolbox calls have been linked against CarbonLib . In general, any particular osax file will probably be intended for one system or the other, not both. You may not be able to tell just by looking; if the Show Package Contents menu item appears in the Finder's contextual menu for an osax, it is
Mac OS X on the one hand, and Classic running under Mac OS X on the other, implement AppleScript separately, but the two are compatible and Apple events travel back and forth between them. This raises the question of how the presence of Classic osaxen affects scripts running under Mac OS X. The answer seems to be that any osax terminology
in code that targets a Classic application
is handled by a Classic osax if possible. You can see this with a
set f to "gromit:Applications (Mac OS 9):SimpleText" tell application f to display dialog "hello" -- clearly the Classic display dialog
On my computer, it is
min monitor depth --
8
But this won't even compile in the Mac OS X Script Editor: set f to "gromit:Applications (Mac OS 9):SimpleText" tell application f to get min monitor depth -- compile-time error: Expected end of line, etc. but found identifier
The simplest solution is to
set f to "gromit:Applications (Mac OS 9):SimpleText"
tell application f to «event aevtgmnd» --
8
But remember, you have to be targeting a Classic application. On its own, the same Apple event will fail: «event aevtgmnd» -- error: «script» doesn't understand the «event aevtgmnd» message |
21.3. Loading Scripting Additions
A scripting addition is not
This architecture has historically caused
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
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
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
|