5

I'm registering an Apple Event handler using NSAppleEventManager:

[[NSAppleEventManager sharedAppleEventManager]
    setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:)
      forEventClass:kInternetEventClass andEventID:kAEGetURL];

My handler method will, of course, receive the event and a reply event:

- (void) handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
    //Open this URL; reply if we can't
}

So, if I need to reply with an error, indicating that I failed in some way to open this URL, how should I use the replyEvent to do that?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370

1 Answers1

8

I have translated the following from the old-style C procedural API described in Apple's legacy document "Apple Events Programming Guide" to Cocoa:

if ([replyEvent descriptorType] != typeNull)
{
    [replyEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithInt32:someStatusCode] forKeyword:keyErrorNumber];
    [replyEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:someErrorString] forKeyword:keyErrorString];
}

See "Returning Error Information" in the "Apple Events Programming Guide" (in the Legacy library).

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • 1
    but how to send this reply back? – jimwan Feb 21 '17 at 05:45
  • @jimwan: Cocoa automatically sends it back. It's handing you an object for you to fill in with details. (Actually, Cocoa is just serving as an in-between. The underlying Apple Event APIs provide the reply event object to Cocoa and automatically sends it back.) – Ken Thomases Feb 21 '17 at 23:06