Associating Protocol handler in Mac OS X

Many tools support an item link feature using protocol; with a given link, I can open the page with the tool that created it.

For example, I can open the DEVONthink page that I made:

open -a "DEVONthink Pro.app" x-devonthink-item://2AD2E3D2-58B5-455F-99D4-C91D68C5F959

Can I teach Mac OS X that the x-devonthink-item is the "DEVONthink" protocol to make the command like this?

open "x-devonthink-item://2AD2E3D2-58B5-455F-99D4-C91D68C5F959"

Solution 1:

Protocol handlers are registered with OS X in application bundles (specifically, in the info.plist file in the bundle's Contents directory). This should be done by the application that supports it. In other words, you should be able to straight-up run the second command and have it open DEVONthink. Based on this page it sounds like that's how it works, but you say it doesn't, so there's a relatively easy way to do it for any program.

If not, or if you just want to register your own handler, here are some steps (modified from Launching External Applications using Custom Protocols under OSX).

Create an AppleScript file that contains the following.

on open location this_URL
    do shell script "open -a 'Applications/DEVONthink Pro.app' this_URL"
end open location

On the second line, you are defining what should happen when your protocol is called. this_URL will be the full URL entered (on the command line or elsewhere), including the protocol. In your case, you want to pass the whole URL including x-devonthink-item into DEVONthink. If you were creating your own protocol, you might only want to pass part of the string, so keep that in mind.

Save the AppleScript as an Application bundle. Once saved, find it on disk, right-click on it, and choose Show Package Contents. Inside the Contents folder there will be a file called info.plist. Open this in a text editor (not TextEdit, as it will almost certainly screw up the file format).

At the bottom of the file will be

</dict>
</plist>

Directly above this, add the following:

    <key>CFBundleIdentifier</key>
    <string>org.personal.dttrick</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>Pass To DEVONthink</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>x-devonthink-item</string>
            </array>
        </dict>
    </array>

Save and double-click on your app. Nothing should happen, and that's fine - but in the background, it's registered the protocol with the OS.

Finally, back in Terminal, run your desired command:

open "x-devonthink-item://2AD2E3D2-58B5-455F-99D4-C91D68C5F959"

DEVONthink should open with the specified item. I don't have DEVONthink and couldn't test it; I am relying on the command you originally provided as working. If not, you'll need to modify the shell script in the AppleScript to reflect what the actual command needs to be.