Skip to content

Instantly share code, notes, and snippets.

@burke
Last active June 21, 2016 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burke/c56db00f8e7110632ed7741540a55829 to your computer and use it in GitHub Desktop.
Save burke/c56db00f8e7110632ed7741540a55829 to your computer and use it in GitHub Desktop.
  1. Build app like this
  2. Open app
  3. open omgscheme://whatever
<!-- ... -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>Whatever!</string>
<key>CFBundleURLSchemes</key>
<array>
<string>omgscheme</string>
</array>
</dict>
</array>
<!-- ... -->
import Cocoa
class AppController: NSObject, NSApplicationDelegate {
let openerPath = "/idk/wherever/you/want"
func applicationWillFinishLaunching(aNotification: NSNotification) {
NSAppleEventManager.sharedAppleEventManager().setEventHandler(
self,
andSelector: #selector(AppController.handleGetURLEvent(_:withReplyEvent:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL)
)
}
func handleGetURLEvent(event: NSAppleEventDescriptor?, withReplyEvent: NSAppleEventDescriptor?) {
defer { NSApp.terminate(self) }
if let event = event {
let url = event.paramDescriptorForKeyword(AEKeyword(keyDirectObject))!.stringValue!
let task = NSTask()
task.launchPath = openerPath
task.arguments = [String](arrayLiteral: url)
task.launch()
task.waitUntilExit()
}
}
}
let app = NSApplication.sharedApplication()
let con = AppController()
app.delegate = con
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment