application:openURLs: equivalent for 10.12?

Hi there,


we implement a custom URL scheme through CFBundleURLTypes and the application:openURLs: delegate method, works perfectly.


Now though the requirement is to add the very same support for macOS 10.12, which, alas, does not support the delegate method; CFBundleURLTypes causes our application to be activated all right, but we don't get the URL to process it further. So far, I was not able to find what delegate method has been used previously for this, before application:openURLs: appeared in 10.13. Can anybody help?


Thanks a lot,

OC

Accepted Reply

Meantime, seems I've succeeded to find the answer myself. Looks like one has to go down AppleEvents; it seems the code to observe for the URL received looks like this:


[NSAppleEventManager.sharedAppleEventManager setEventHandler:self andSelector:@selector(handleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

and the URL then can be obtained from the event like this:


[event paramDescriptorForKeyword:keyDirectObject].stringValue


We'll see soon as I am able to test in 10.12...

Replies

Did you try using NSWorkSpace ?

   NSWorkspace.shared().open(URL(string: "https://forums.developer.apple.com/thread/122581")!)


Or one of those 2 others:


func open(URL, options: NSWorkspace.LaunchOptions, configuration: [NSWorkspace.LaunchConfigurationKey : Any]) -> NSRunningApplication


func open([URL], withApplicationAt: URL, options: NSWorkspace.LaunchOptions, configuration: [NSWorkspace.LaunchConfigurationKey : Any]) -> NSRunningApplication

Perhaps I just don't properly understand the darned Swift thing, but far as I can say, this is not what I need.


I do not need to open a URL from my application; I need my application to be able to process a URL with a custom scheme, opened from elsewhere (e.g., Safari, Mail, etc.)

Sorry if I missed your point.


Could you detail the useage scenario:

"my application to be able to process a URL with a custom scheme, opened from elsewhere (e.g., Safari, Mail, etc.)"


What do you mean by opened from elsewhere ?

Well, you open in Safari or Mail or essentially wherever (e.g., from another application through the aforementioned NSWorkspace APIs) an URL, say, „ocsoftware://show.logs“, and my application — if installed, of course — gets activated and shows logs. (The actual usage somewhat differs, would be a bit too complex to explain here and is completely irrelevant to the problem; but this is the principle.)

Meantime, seems I've succeeded to find the answer myself. Looks like one has to go down AppleEvents; it seems the code to observe for the URL received looks like this:


[NSAppleEventManager.sharedAppleEventManager setEventHandler:self andSelector:@selector(handleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

and the URL then can be obtained from the event like this:


[event paramDescriptorForKeyword:keyDirectObject].stringValue


We'll see soon as I am able to test in 10.12...

What about application:openFiles: ?

Yes, that's the answer I was going to suggest. I don't know for sure, but I believe that it's not just a matter that the requisite app delegate wasn't there (or was different) pre-10.13, it's that the underlying support didn't arrive until the "openURLs" API arrived too. If that's true, then the "old" way to do it would have been to handle the AppleScript event.


I think you've come up with the right answer. Even if there's a slightly different alternative, what you're doing should be fine.

I don't know how Apple does it, but seems to me quite probable that the underlying support is the very NSAppleEventManager. The 10.13 news then would be just that the NSApplication init-time checks whether there's a CFBundleURLTypes set, if so, adds its own URL event handler to NSAppleEventManager, and upon getting the event sends the openURLs delegate message. Wouldn't it be the easiest and simplest solution?


Anyway, meantime, my client tested in 10.12 and told me it works, so it seems the right answer indeed.


Thanks!