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 juce_mac_MessageManager.mm 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..

Sorry to revive this very, very old thread. Buuuuut - how does one use this? I see that it’s built into juce_mac_MessageManager.mm but I’m not sure how to both set up Xcode to allow for this, or how to get at the string from my app. It looks like it’ll come in as a string to the anotherInstanceStarted function, is that correct?

Yes, you need to override JUCEApplication::anotherInstanceStarted() to catch it.

To add a URL type that’s in the Info tab for your target. You only have to add the “Identifier” (a text description) and “URL Schemes” (the protocol name).