Make Your Own URL Protocol and Handler
July 17, 2013 4 Comments
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.)
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:
(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!