< Previous PageNext Page > Hide TOC

About Apple Events

This chapter provides an overview of when and how applications use Apple events, with links to more detailed information on those topics. It also provides a brief description of the framework and language support available for working with Apple events in Mac OS X.

An Apple event is a type of interprocess message that can encapsulate commands and data of arbitrary complexity. Apple events provide a data transport and event dispatching mechanism that can be used within a single application, between applications on the same computer, and between applications on different computers connected to a network. The Mac OS uses Apple events to communicate with applications.

Apple events are part of the Open Scripting Architecture (OSA), which provides a standard and extensible mechanism for interapplication communication in Mac OS X. The OSA is described in AppleScript Overview.

Note: Apple events are not always the most efficient or appropriate method for communicating between processes. Mac OS X offers other mechanisms, including distributed objects, notifications, sockets, ports, streams, shared memory, and Mach messaging. These mechanisms are described in “Interprocess Communication” in System-Level Technologies in Mac OS X Technology Overview.

In this section:

A Quick Look at Working With Apple Events
How to Use Apple Events
Framework and Language Support


A Quick Look at Working With Apple Events

Apple events are designed to provide a flexible mechanism for interprocess communication. An Apple event specifies a target application (or other process) and provides a detailed description of an operation to perform. The operating system locates the target, delivers the event and, if necessary, delivers a reply Apple event back to the sender. While simple in concept, this mechanism provides a basis for powerful interaction between processes and for automating tasks that use multiple applications.

When Applications Use Apple Events

An application is most likely to work with Apple events for the following reasons:

Apple Event Terminology

A scriptable application specifies the terminology that can be used in scripts that target the application. There are currently three formats for this information:

For more information on these formats, including pointers to additional documentation, see "Scriptable Applications" in Open Scripting Architecture in AppleScript Overview.

Sending and Receiving Apple Events

Applications typically use Apple events to request services and information from other applications or to provide services and information in response to such requests. In client-server terms, the client application sends an Apple event to request a service or information from the server application. The recipient of an Apple event is also known as the target application because it is the target of the event. A client application must know which kinds of Apple events the server supports.


Figure 1-1  Client and server applications communicating with Apple events

Client and server applications communicating with Apple events

Figure 1-1 shows two applications communicating with Apple events. The client uses Apple Event Manager functions to create and send an Apple event to the server, the FileMaker Pro database application. The event might, for example, request employee information from a payroll database. FileMaker Pro uses other Apple Event Manager functions to extract information from the event and identify the requested operation. Depending on the event, FileMaker Pro may need to add a record, delete a record, or return specified information in a reply Apple event.

The most common Apple event client is a script editor application executing an AppleScript script. Statements in a script that target an application may result in Apple events being sent to the application. Another common client is the Mac OS, which sends Apple events to applications to open documents and perform other operations.

The most common servers are scriptable applications and scriptable parts of the Mac OS, such as the Finder and the System Events application (located in /System/Library/CoreServices). You can read more about script editors and scriptable applications in AppleScript Overview.

Responding to Apple Events

Your application should be prepared to respond to Apple events sent by the Mac OS, as well as to other Apple events the application supports. An Apple event typically contains information that specifies the target application, the action to perform, and optionally the objects on which to operate. For example, Figure 1-2 shows an open documents Apple event sent by Mac OS X to the AppleWorks application. This type of Apple event provides a list of files for the target application to open.


Figure 1-2  The Mac OS sending an open documents Apple event

The Mac OS sending an open documents Apple event

For an application to handle a specific Apple event such as the open documents event, it must register with the Apple Event Manager a function that handles events of that type. The Apple Event Manager dispatches a received Apple event to the handler registered for it. An Apple event handler is an application-defined function that extracts pertinent data from an Apple event, performs the requested action, and if necessary, returns a result.

How to Use Apple Events

The steps your application takes in working with Apple events will differ, depending on whether it is responding to Apple events or creating and sending them.

Steps for Responding to Apple Events

To respond to Apple events in your application, you perform steps like the following:

For guidelines and sample code for performing these steps, see “Apple Event Dispatching” and “Responding to Apple Events.”

Steps for Creating and Sending Apple Events

To create and send Apple events in your application, you perform steps like the following:

For guidelines and sample code for performing these steps, see “Two Approaches to Creating an Apple Event” and “Creating and Sending Apple Events.”

Framework and Language Support

You work with Apple events primarily through the API defined by the Apple Event Manager, which is documented in Apple Event Manager Reference. This API is implemented by the AE framework, a subframework of the Application Services framework, and is made available through headers written in the C programming language.

Note: The Apple Event Manager is part of the Open Scripting Architecture. Some Apple-event related functions and constants are not defined in the AE framework—they are defined in other frameworks, as described in Open Scripting Architecture” in AppleScript Overview.

Carbon applications that work with Apple events, whether written in C or C++, typically call Apple Event Manager functions directly. Apple also provides C sample code, such as MoreOSL, to help in the implementation of scriptable Carbon applications.

Cocoa applications are written in Objective-C or Java. The Cocoa application framework provides built-in support for AppleScript scripting that allows many applications to be scriptable without working directly with Apple Event Manager functions. The Cocoa application framework also includes classes such as NSAppleEventDescriptor, for working with underlying Apple event data structures, and NSAppleEventManager, for accessing certain Apple Event Manager functions.

However, because Objective-C is a super set of the C language, Cocoa applications written in Objective-C can call Apple Event Manager functions directly and use any of the mechanisms described in this document. For example, a Cocoa application might use Apple Event Manager functions to perform operations that are not currently supported by the Cocoa framework, such as directly sending an Apple event. For details on writing a scriptable Cocoa application, see Cocoa Scripting Guide.



< Previous PageNext Page > Hide TOC


© 2005, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.