Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Installing an Apple Event Handler

Your application, whether scriptable or not, can install Apple event handlers directly. This generally makes sense in the following cases:

To install an Apple event handler, you invoke this method of the NSAppleEventManager class:

setEventHandler:andSelector:forEventClass:andEventID:

The signature for the new handler should match the one shown in Listing 10-2.

Listing 10-2  Signature of an event handler function

- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent;

A good place to install event handlers is in the applicationWillFinishLaunching: method of the application delegate. At that point, the Application Kit has installed its default event handlers, so if you install a handler for one of the same events, it will replace the Application Kit version.

In this section:

Installing a Get URL Handler
Implementing the Get URL Handler


Installing a Get URL Handler

Listing 10-3 shows how you could install a handler for the get URL Apple event.

Note: To work with URL events, your application will have to specify one or more keys for the CFBundleURLTypes dictionary in its information property list file. These keys are described in the section "CFBundleURLTypes” in Property List Key Reference in Runtime Configuration Guidelines.

Listing 10-3  Installing an Apple event handler in a Cocoa application

NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];// 1
[appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];// 2

Here’s what the code in Listing 10-3 does:

  1. It gets a reference to the shared Apple Event Manager object.

  2. It invokes a method of that object to install the new handler, passing:

    • A reference to the delegate object, self, which will handle the event.

    • A selector for the new get URL handler (shown in Listing 10-4).

    • The event class constant for the Apple event (from the header HIServices/InternetConfig.h in the Application Services framework).

    • The event ID constant for the Apple event (from the header HIServices/InternetConfig.h in the Application Services framework).

    If an event handler is already installed for the specified event class and event ID, it is replaced.

Implementing the Get URL Handler

Listing 10-4 provides a template for the event handler. This code resides in the implementation of your application delegate. After this handler has been installed, it is invoked whenever the application receives a get URL Apple event. The Apple event is passed to the handler as an instance of NSAppleEventDescriptor.

Listing 10-4  Implementation of a get URL Apple event handler

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    // Extract the URL from the Apple event and handle it here.
}

The implementation details are left to you, but they require using methods of the NSAppleEventDescriptor class to extract the URL from the direct parameter of the Apple event, then performing the required operation with it (typically displaying the referenced page in a window). For a similar example, see Listing 10-1.



< Previous PageNext Page > Hide TOC


Last updated: 2008-03-11




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.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice