Your application, whether scriptable or not, can install Apple event handlers directly. This generally makes sense in the following cases:
You want to customize the default handling of one of the events for which the Application Kit installs a handler, but you can't do so in the standard way, by implementing or overriding the methods described in “Apple Events Sent by the Mac OS.”
For example, you might want to install a handler for the open contents Apple event to override its default behavior.
You have not made your application scriptable, but you need to handle certain Apple events not supported automatically by the Application Kit.
If you find yourself handling more than a few Apple events this way, consider making your application scriptable to take advantage of Cocoa scripting's built-in support for handling Apple events.
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.
Installing a Get URL Handler
Implementing the 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:
It gets a reference to the shared Apple Event Manager object.
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.
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.
Last updated: 2008-03-11