LinkCatcher

Newest version: 1.0 | Released: 04 November, 2014
OS X versions tested: 10.9 (Mavericks), 10.10 (Yosemite)

Download

Description

Normally you have a default browser set and all links clicked in any application get opened in that browser. This is fine most of the time, but sometimes you're in an environment where you want links clicked in some applications to get opened in a browser other than your default.

For example: On my work computer, I use Google Chrome most of the time, but like to separate randomly accumulated links from twitter into Safari. Normally this means either 1) clicking the link, seeing it open in Chrome, copying the URL into Safari, then closing the Chrome tab or 2) copying the link from the application, switching to Safari, and pasting the URL in. Both of these workflows are disruptive.

LinkCatcher is a simple application that "catches" all links clicked and determines which browser it should go to. Feel free to grab the file from the download link above and make changes or copy the code provided below.

How to set up

OS X 10.10 requires additional work to get this to operate. Make sure to read all of the setup instructions.

  1. Download the LinkCatcher zip file and extract the application.
  2. Drag that into your Applications folder.
  3. Double click LinkCatcher once. (You may not see anything happen. This may even be optional, but it should register the application as being able to handle links on your system. Depending on OS version and security settings, you may need to manually open the application with a right-click and Open, or go into System Preferences and the Security panel then click to open the recently blocked application right after you first try double clicking it.)
  4. In Safari's preferences, set LinkCatcher as your default browser (you may need to choose the select option and then manually select the application bundle).

Continue for Yosemite (10.10) only:
In OS X 10.10, Apple has moved the system control over browser selection from within Safari to under the General tab in System Preferences. This makes perfect sense. However, here Apple has removed the ability to arbitrarily select an application and seems to have limited the selection to applications presenting themselves as browsers which are code signed by their developers. This tool isn't code signed, so the setting won't let you apply it. Here is a workaround:

  1. Go download RCDefaultApp, here. (It's very old, but still works.)
  2. Once you download the application, double click the preference pane to install it. You may get a security alert for the unsigned application, so go ahead and authorize it.
  3. In System Preferences, go to the new "Default Apps" panel in the bottom row.
  4. Ignore the "Internet" tab, as the General area in System Preferences seems to block this from actually changing. Instead, go to the "URLs" tab, scroll down to http, and select LinkCatcher.app. Do the same for https.

That should do it!

Notes:

  • If you make changes to an already-working copy of the applcation, for example to alter default settings, according to guidelines below, it should work in place with the changes automatically applied.
  • If you replace the application with a newly downloaded version from another "publisher" (essentially just a computer other than mine) you may need to manually open it once more to authorize that new publisher on your system.
  • If you install a new copy of the application rather than modifying the code in your existing installation, and it seems not to work initially, try manually launching to check if re-authorizing is necessary then attempt toggling LinkCatcher off and on as your default browser/link handler.

How it works

LinkCatcher is a simple AppleScript application you set as your Mac's default browser. Any time you click a link in an application, that link is handed off to the OS, which then passes the URL to your default browser. LinkCatcher sees this link, detects the frontmost application (which would be the one you just clicked), and chooses which browser to send the link to accordingly.

Default Configuration

The default configuration opens links clicked in Tweetbot or Messages in Safari, while Chrome continues to be used for links from other sources. Check out the next section to see how to modify this behavior.

The Code

There are two important chunks to this application. You can either do this yourself, or edit the application download itself using AppleScript Editor on your Mac.

AppleScript

This is the main chunk of the application. You can start a new AppleScript by going to your Applications folder, Utilities, then AppleScript Editor. It's pretty simple logic:


on open location the_url
    set trigger_applications to {"Tweetbot", "Messages"}
    set triggered_browser to "Safari"
    set default_browser to "Google Chrome"
    
    tell application "System Events"
        set frontmost_app to name of application processes whose frontmost is true
    end tell
    
    if frontmost_app is in trigger_applications then
        tell application triggered_browser
            activate
            open location the_url
        end tell
    else
        tell application default_browser
            activate
            open location the_url
        end tell
    end if
end open location

Once you have that entered, make sure to save as an Application. You will be able to continue to edit and re-edit it afterward. You can feel free to make some small modifications.

trigger_applications is an array of the names of all applications you'd like to "catch" links for and redirect away from your default browser. These are strings between quotation marks, separated by commas.
Defaults: Tweetbot, Messages

triggered_browser is your secondary browser. The one you only want to direct links to if those trigger_applications are the source of the click.
Default: Safari

default_browser is your primary browser. The one you want all other links to go to.
Default: Google Chrome

The rest of the application just detects what the frontmost application is, determines if it's in the list of your trigger applications, and passes the URL to the proper browser accordingly.

Info.plist

In OS X, applications have "property list" files, which store information about a given application's capabilities. Along with many other things, this is how an application tells your Mac what files it's capable of opening. If you're following along at home rather than using my application, you will need to add the following into your plist, which you can find by right-clicking the saved application and clicking Show Package Contents, then going to Contents and opening Info.plist:


    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>Web site URL</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>http</string>
                <string>https</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleURLName</key>
            <string>Local file URL</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>file</string>
                <string>local</string>
            </array>
        </dict>
    </array>
    
    <key>LSUIElement</key>
    <true/>

Adding those two keys to the plist just tells OS X LinkCatcher:

  • can handle http://, https://, file:, and local: URL Schemes (the CFBundleURLTypes key and its array value).
  • shouldn't have a dock icon (LSUIElement and its true boolean value).

Known/Possible Issues

  • Sometimes it seems as if, when Chrome is not already opened and therefore is launched by LinkCatcher, that Chrome is merely activated but the link isn't opened.
    • You may just need to change Windows. This is an issue with a delay between Chrome's launch and when it opens your previous windows. It first opens a brand new window with your desired link, then restores the previous session, so the new window gets thrown behind the previous session.
    • Everything works exactly as expected in Safari in both the already-open and not-launched states, as well as Chrome in the already-open state.
  • I can imagine noticeable delays in your clicks that are undesirable if you're on a slower system, especially if running off a spinning hard disk.
    • This is adding a tiny application's launch between clicking a link and opening that link in a browser. Because the application launches and closes each time, it's possible OS X may flush the application out of memory and require a seek on your hard drive. I expect this to be minor if it's ever really noticeable.
    • If this is a problem and you want to force LinkCatcher to stay in memory, you can create a new AppleScript and save again, selecting the Stay open after run handler option. This should leave the application open in the background, which means it should stay in RAM.
      • Warnings:
        • I haven't actually tried this. It should work, but don't come at me if your computer explodes instead.
        • Because it would be constantly running with no dock icon, you'll only be able to quit the application through terminal or Activity Monitor.