AppleScript_ The Definitive Guide - Matt Neuburg [27]
First, open the Console; that's where certain Apple events are going to be reported to us. Next, go into the Terminal and enter the following (I'm assuming your shell is bash, the default in both Panther and Tiger):
% export AEDebugSends=1; export AEDebugReceives=1
These commands turn on the environment settings that cause Apple events to be intercepted and reported. These settings will apply only within this shell session, and only with respect to applications that are launched from within this process. So, let's launch one:
% open /Applications/Safari.app
Now any Apple events sent to Safari will be logged. Let's send one. Possibly you were unaware that the Unix open command is implemented in Mac OS X with Apple events; you're about to discover that it is. Say this:
% open http://www.apple.com
(I'm assuming here that Safari is your default browser.)
Within the Terminal, the process started by the open command sends an Apple event, and this fact is reported within the Terminal. Immediately afterwards, Safari receives this Apple event, and this fact is reported within the Console. The two Apple events are exactly the same event. As reported in the Console, when Safari receives it, it will look something like this:
AE2000 (556): Received an event:
------oo start of event oo------
{ 1 } 'aevt': GURL/GURL {
return id: 38666240 (0x24e0000)
transaction id: 0 (0x0)
interaction level: 112 (0x70)
reply required: 0 (0x0)
target:
{ 1 } 'psn ': 8 bytes {
{ 0x0, 0x3e0001 } (open)
}
optional attributes:
< empty record >
event data:
{ 1 } 'aevt': - 1 items {
key '----' -
{ 1 } 'TEXT': 20 bytes {
"http://www.apple.com"
}
}
}
------oo end of event oo------
The Apple event is displayed in a slightly different text format from Example 3-1. I don't know the official name for this text format, so let's call it AEDebug format . AEDebug format is more verbose (and more informative) than AEPrint format, but the same basic elements are clearly present: this is a GURL\GURL event with just one parameter, namely the direct object designated by ---- (which turns out to be the actual URL that Safari was asked to open).
Another way to send an Apple event from the Terminal is to use AppleScript directly, by way of the osascript command (already mentioned under "Unix" in Chapter 2, and formally discussed in Chapter 25). In the Terminal, enter this:
% osascript -e 'tell app "Finder" to get disks'
This command causes the Terminal to spew out large amounts of information, most of it having to do with the mechanics of compiling and running AppleScript code, and culminating in the Apple event sent to the Finder, along with the Finder's reply. It should look something like this:
AE2000 (811): Sending an event:
------oo start of event oo------
{ 1 } 'aevt': core/getd {
return id: 53149700 (0x32b0004)
transaction id: 0 (0x0)
interaction level: 64 (0x40)
reply required: 1 (0x1)
target:
{ 2 } 'psn ': 8 bytes {
{ 0x0, 0xc0001 } (Finder)
}
optional attributes:
{ 1 } 'reco': - 1 items {
key 'csig' -
{ 1 } 'magn': 4 bytes {
65536l (0x10000)
}
}
event data:
{ 1 } 'aevt': - 1 items {
key '----' -
{ 1 } 'obj ': - 4 items {
key 'form' -
{ 1 } 'enum': 4 bytes {
'indx'
}
key 'want' -
{ 1 } 'type': 4 bytes {
'cdis'
}
key 'seld' -
{ 1 } 'abso': 4 bytes {
'all '
}
key 'from' -
{ 4 } 'null':