URL handlers on OS X

Friday, 20. 04. 2012  –  Category: sw

Update 2012-04-25 Corrected the plist key to CFBundleIdentifier.

I’ve often wondered about how to do this without monkeying in Objective C. It’s doable in (ugh) Applescript with some property list editing. Whimsical XKCD handler such that xkcd://627 behaves like http://xckd.com/627:


on open location uri
  set delimiter to AppleScript's text item delimiters
  set AppleScript's text item delimiters to "//"
  set comic to text item 2 of uri
  set page to "http://xkcd.com/" & comic
  tell application "Safari"
    activate
    open location page
  end tell
  set AppleScript's text item delimiters to delimiter
end open location

Save as an Application rather than a Script. Open its info.plist and add


 <key>CFBundleIdentifier</key>
 <string>uk.co.zomo.xkcd-handler</string>
 <key>CFBundleURLTypes</key>
 <array>
  <dict>
   <key>CFBundleURLName</key>
   <string>XKCD helper</string>
   <key>CFBundleURLSchemes</key>
   <array>
    <string>xkcd</string>
   </array>
  </dict>
 </array>

To nudge OS X to notice changed a plist:


/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f ~/Library/Scripts/xkcd-handler.app

Links:

Comments are closed.