Skip to content

Instantly share code, notes, and snippets.

Created September 15, 2016 01:46
Show Gist options
  • Save anonymous/78ddfabbf80d25ac5a3fd43b6e42edbf to your computer and use it in GitHub Desktop.
Save anonymous/78ddfabbf80d25ac5a3fd43b6e42edbf to your computer and use it in GitHub Desktop.
Handle URIs with custom schemes on macOS
// After your application launches, register to handle Apple Events
func applicationDidFinishLaunching(_ aNotification: Notification)
{
let appleEventManager = NSAppleEventManager.shared()
appleEventManager.setEventHandler(self,
andSelector: #selector(AppDelegate.handleGetURLEvent(_:withReplyEvent:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL))
}
// When the Apple Event is handed to your app, get the URI from it
func handleGetURLEvent(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor)
{
let uriString = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue!
print("\(uriString)")
}
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>Fyrestead</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fyrestead</string>
</array>
</dict>
</array>
import Cocoa
NSWorkspace.shared().open(URL(string: "yourscheme://path/to/whatever")!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment