by Chris Reed
Unknown to most people, AppleScript supports a syntax that can be used to send arbitrary events to any application. This syntax is partially present so that an event command stored in a compiled script can be displayed even if the application's dictionary has changed. But it can also be used for your own nefarious purposes.
You can put events described with this syntax in any tell block to send to any application.
The arbitrary event syntax uses characters called "chevrons" to identify the keywords in an event declaration. You can type the chevrons on the Mac US English keyboard by hitting opt-\ (for "«") and opt-shift-\ (for "»").
The basic event sending syntax looks like this:
«event ClasIden»
where "Clas" is the event class and "Iden" is the event identifier. The class and identifier must each have exactly four characters, and no characters in between.
«event ClasIden» 10
This will send an integer 10 as the direct object. Any value type or variable may be used. Direct objects are entirely optional.
Additional parameters can be sent with the event by using the form given below:
«event ClasIden» given «class Parm»:"value"
Here, "Parm" is the identifier for the parameter, also a four-character code. The value after the colon may be any AppleScript value or variable. If you need the value to be a specific type, use an as operator. Multiple parameters can be passed by separating the parameters with commas.
For either the direct object or additional parameters, you can specify objects in the target application by four-character code and either numeric index or name. This looks like:
«event openaevt» «class wndo» 1
The extra "class" identifier in this event declaration specifies an object of type "wndo", perhaps a window in the target application, with a numeric index of 1. If the value had been, for example, the string "Untitled", the "wndo" would have been selected by name.
Using this syntax for additional parameters looks like this:
«event openaevt» given «class prm1»:«class myob» "fred"
An example Apple event declaration using all of the above mentioned syntax options, and passing a list for a parameter value, looks like this:
«event openaevt» «class wndo» 1 given «class prm1»:{"a", "b", "c"}, «class prm2»:«class myob» "mikey"
If you would like to see what events in a compiled script you already have available would look like using this syntaxoften good for reproducing events during debuggingfollow the steps below.