Make Your Own URL Protocol and Handler

One of the people I follow on twitter recently posted the following:

It would be really useful for helpdesk if you could link to various OS X system elements, hyperlink style. Like System Preferences, etc.

That got me thinking. I know that apps can register themselves as handlers for certain url types. Panic’s Transmit, for example, can handle ftp urls: if you type “ftp://some.server.com/” into Safari, Panic will launch and try to connect to that URL. Since the pros can do it, I figured it would be possible to create a new url protocol name so that if I typed “syspref://Network” into Safari, a simple app could parse that URL and tell System Preferences to open directly to the Network pane.

Turns out I was right. With the help of this page on macosautomation.com, here’s how I did it.

Open AppleScript Editor and paste the following code into a new document:

This code should be fairly self-explanatory. I chose to have URLs in the format “syspref://[PrefPane]” to keep things simple. The script then attempts to open that pref pane in one of three location: /System/Library/PreferencePanes, /Library/PreferencePanes, and ~/Library/PreferencePanes. The name you put after the double slashes needs to match the filename (sans extension) of the pref pane you want to open. If the pref pane has a space in it, .e.g. “Flash Player”, use an underscore instead of a space: “Flash_Player”. The script translates underscores to escaped spaces to keep things compatible with posix paths. The syspref: urls are not case sensitive.

Save this script as an application in your /Applications folder. I called mine syspref-handler.app. Once the app is saved, you’ll see a new icon on the right side of the editor window’s toolbar labeled Bundle Contents. Click the button to reveal the bundle’s contents. All you need to do here is change the default Bundle Identifier content to something less cryptic. I use org.pmbuko.syspref-handler to keep it simple. (This step is really optional, but I like to be thorough.)

syspref_script

Next, navigate to the /Applications folder in the Finder, then right-click on syspref-handler.app and select Show Package Contents. Now go into the Contents folder and open Info.plist in your favorite text editor. Add the following lines to the file, making sure to keep all existing key/string pairs intact:

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLName</key>
    <string>SysPref Handler</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>syspref</string>
    </array>
  </dict>
</array>

These lines are what tell the OS to direct all URLs using the syspref: protocol to our new app. Save the changes and close Info.plist. Now you need to register the application with Launch Services. The easiest way to do that is by double-clicking on it. It’ll look like nothing happened, but that’s fine. Now, open Safari and type the following URL into the address bar and hit Enter:

syspref://network

(I’d put a link here for you to click on, but WordPress is being stupid and won’t let me use syspref: as a protocol.)

You may be warned about launching the app for the first time. Tell it yes, you’re sure. If all went as planned, System Preferences should open directly to the Network pane. Test the URL protocol with other pref panes, too.

Going back to the tweet that started this all, if you’re a sysadmin, you might consider adding this app to your standard Mac deployment. It would allow your helpdesk staff to include “syspref://” urls in their communication with users.

Got feedback? Let me know in the comments!

About these ads

About pmbuko
mac/linux geek

4 Responses to Make Your Own URL Protocol and Handler

  1. Stephan Peterson says:

    Peter, you might consider a slight tweak to a line near the end of your AppleScript code:

    set the pref_pane_path to “/System/Library/PreferencePanes/” & quoted form of pref_pane & “.prefPane”

    While none of Apple’s pref panes have spaces in their filenames, it’s possible that future ones could or third-party ones might. This would allow for that.

    • pmbuko says:

      You’re right. And while I’m at it, I should also have the script look for matching pref panes in /Library/PreferencePanes and ~/Library/PreferencePanes, too. I’ll update the post with these changes soon.

      • Stephan Peterson says:

        Yes, another good thing to consider, especially for third-party pref panes.

        • pmbuko says:

          Ok, I overhauled the script a bit. I didn’t end up needing to use “quoted form of”. Since URLs can’t/shouldn’t have spaces in them, I chose to use underscores in their place. The script replaces them with escaped spaces.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 481 other followers

%d bloggers like this: