title: Open Cocoa App via URL Scheme
body: <p>My question is identical to <a href="https://stackoverflow.com/questions/24080210/how-to-capture-the-url-used-to-launch-an-osx-application-via-urlscheme-in-swift">this question</a> but it hasn't been updated in a while and Swift has changed a lot since it was asked so the answer might need to be refreshed. All I want to do is capture the url used to open my cocoa app, but the code never gets executed in my handler function. Errors are thrown in Console.app when I launch my application via the custom url scheme. Here are the errors:</p>
  
  <pre><code>-[MyApp.AppDelegate handleGetURLEvent:replyEvent:]: unrecognized selector sent to instance 0x60c0000039b0
  -[MyApp.AppDelegate handleGetURLEvent:replyEvent:]: unrecognized selector sent to instance 0x60c0000039b0
      OSErr AERemoveEventHandler(AEEventClass, AEEventID, AEEventHandlerUPP, Boolean)(spec,phac handler=0x7fff576b7f15 isSys=YES) err=0/noErr
  -[MyApp.AppDelegate handleGetURLEvent:replyEvent:]: unrecognized selector sent to instance 0x60c0000039b0
  -[MyApp.AppDelegate handleGetURLEvent:replyEvent:]: unrecognized selector sent to instance 0x60c0000039b0
      OSErr AERemoveEventHandler(AEEventClass, AEEventID, AEEventHandlerUPP, Boolean)(GURL,GURL handler=0x7fff5661d680 isSys=YES) err=0/noErr
  LSExceptions shared instance invalidated for timeout.
  </code></pre>
  
  <p>And my bare bones app:</p>
  
  <p>Create a new Cocoa app named MyApp. In Info > URL Types, enter my app's bundle identifier in the Identifier field, and <code>blue</code> (for example) in the URL Schemes field. Add the following two functions to the AppDelegate class:</p>
  
  <pre><code>func applicationWillFinishLaunching(_ notification: Notification) {
      let appleEventManager: NSAppleEventManager = NSAppleEventManager.shared()
      appleEventManager.setEventHandler(self, andSelector: Selector(("handleGetURLEvent:replyEvent:")), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
  }
  
  func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
      NSLog("heyo!")
  }
  </code></pre>
  
  <p>Build and run my app. Quit it, then in Safari type <code>blue://whatever</code> in the address bar and hit return. The app opens, but the NSLog doesn't show up in Console.app and instead I get the errors I mentioned above. I would love to be stuck on how to parse the url, but I can't even get to that part yet. I'm running Xcode 8.3.3 (8E3004b) and Swift 3.1. Do you all get the same result as me? Am I calling the handler function wrong?</p>