Apple - How to access a clicked URL in a URL handler application created in Automator

The way URL opening in Mac OS X is actually more complicated than you would think. When you click a URL, Mac OS X does not just pass the browser the URL to open; instead, it sends it an Apple Event, with the ID kAEGetURL, containing the URL.

I never really used Automator (it's just way too slow and limited for my typical use), so I'm not sure about this, but I doubt it has the ability to handle Apple Events. Therefore you won't be able to use Automator to achieve what you want.

You'll therefore have to resort to a normal Objective-C app in Xcode.

The two basic things you need to do are:

  1. register your app for receiving the kAEGetURL event, and set the CFBundleURLTypes in your app's Info.plist to include http and https.

  2. Implement a method handling the kAEGetURL event, and make it copy the URL to the clipboard.

These two things are relatively straightforward to do in Objective-C; to illustrate, I've created a sample application that does this. You can view it at https://github.com/houbysoft/short/tree/master/Copy%20URL%20to%20Clipboard. The most important file is https://github.com/houbysoft/short/blob/master/Copy%20URL%20to%20Clipboard/Copy%20URL%20to%20Clipboard/AppDelegate.m, there you can see how to register for the event (this is done in the applicationWillFinishLaunching: method) and how to copy it to the clipboard (this is done in the getUrl:withReplyEvent: method).

If you're feeling lazy and just want to see that this works, grab this file, extract the application from it, set that as the default web browser (open Safari Preferences, and browse for that app in the Default web browser field). Then click on your link in Skype, and it should be copied straight to your clipboard.