Launching an OSX app from a URL protocol


In Xcode you can set "URL Types" for your app so a URL with that type (myType://myUrlToSendToApp) will open the app.  On Windows the URL arrives on the command line, but in a OS X JUCE app nothing happens.  This is what I did to fix that:


In add these lines to the AppDelegateClass() constructor:

  addMethod (@selector (applicationWillFinishLaunching:), applicationWillFinishLaunching, "v@:@");
  addMethod (@selector (getUrl:withReplyEvent:), getUrl_withReplyEvent, "v@:@@");

And in the private: section below that, add:

  static void applicationWillFinishLaunching (id self, SEL, NSApplication* app, NSNotification*)
    //Register as a URL handler
    [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

  static void getUrl_withReplyEvent (id /*self*/, SEL, NSAppleEventDescriptor* event, NSAppleEventDescriptor* replyEvent)
    NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
      app->anotherInstanceStarted (quotedIfContainsSpaces (urlStr));


Maybe there is a better way to send the URL to the app, but it looks like it arrives too late to be used as the initial command line.


Thanks for the suggestion! I've added something to do this now if you want to try it..