I am working on an OS X application which gets called by other applications via Custom URL Scheme. Below code allows me to capture the incoming URL successfully. However I also need to get the name or bundle_id of the source application.
This document explains how it is done for UIKit, Defining Custom URL Scheme , where it mentions let sendingAppID = options[.sourceApplication]. How can I do the same on an OS X application ?
EDIT: Found a kind of workaround, first get the pid of the source application using let pid = event.attributeDescriptor(forKeyword: AEKeyword(keySenderPIDAttr))!.int32Value then iterate over workspace.runningApplications to get localised_name or bundle_id for the matching pid. Doesn't look very elegant but works. Any better way of doing this ? i.e. getting bundle_id or name directly via event handler ?
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillFinishLaunching(_ notification: Notification) {
NSAppleEventManager
.shared()
.setEventHandler(
self,
andSelector: #selector(handleURL(event:reply:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL)
)
NSLog("Debug: Handle Set")
}
@objc func handleURL(event: NSAppleEventDescriptor, reply: NSAppleEventDescriptor) {
if let path = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue?.removingPercentEncoding {
NSLog("Opened URL: \(path)")
}
}
}