AppleScript Arbitrary Event Syntax

by Chris Reed

Introduction

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.

Basic Form

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.

Direct Object

To pass a direct object parameter, append it after the event declaration. Like this:
«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.

Parameters

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.

Specifying Objects

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"

Displaying Events in This Form

If you would like to see what events in a compiled script you already have available would look like using this syntax—often good for reproducing events during debugging—follow the steps below.

  1. Make sure the script is compiled and not a text source file.
  2. Find the application or applications that the script is targeted at.
  3. Either delete these applications, dismount the volume the reside on, or put them in an archive of some form. They must be somehow hidden so Script Editor and AppleScript cannot find them.
  4. Open your script in Script Editor.
  5. When asked to find a missing application, click the "Browse" button.
  6. Choose any random application that does not support the same events as the original target application.
  7. After each missing application is "found", the script will be opened an shown using this syntax. If there happen to be some events in the script that are handled by the application you chose in place of the original target application, they will be shown using normal AppleScript syntax.