Wishful Coding

Didn't you ever wish your
computer understood you?

Open external links in running browser

Open external links in running browserAs a web developer I have like 5 browsers installed which I use regularly. That is, not on daily basis, but for occasional testing and browsing. What annoys me most about this is that when I click a link in another application, no matter what browser I'm currently running, it opens a fresh Safari window. The ideal solution would be to open any urls with the browser I'm using at that moment, no mater which browser that is and which one is set as the default browser. I have written a piece of Applescript that will do just this, you might need to edit this for the browsers which you are using though. Below you'll find a zip file containing the app bundle, and all the scripts needed to generate it yourself.

Browser Loader

To edit the browsers you use, open "/Applications/Utilities/AppleScript Editor.app" and then open the app bundle, this will reveal the script below, where you can make adjustments to the browsers used and their order. If you want do do this yourself, or the app bundle does not work for you for some reason, this is the actual Applescript I used:
on open location the_url
        tell application "System Events"
                set browser_apps to the name of every process whose visible is true
                if "Opera" is in browser_apps then
                        tell application "Opera"
                                open location the_url
                                activate
                        end tell
                else if "firefox-bin" is in browser_apps then
                        tell application "Firefox"
                                open location the_url
                                activate
                        end tell
                else if "Google Chrome" is in browser_apps then
                        tell application "Google Chrome"
                                open location the_url
                                activate
                        end tell
                else
                        tell application "WebKit"
                                open location the_url
                                activate
                        end tell
                end if
        end tell
end open location
Open external links in running browserCopy this file to the Applescript editor and hit Save, before you save, choose to save as an app bundle and make sure to check the "Stay Open" box. Now we need to modify the app bundle to make it eligible for url opening. To do this, right-click on the app and select "Show Package Contents", go to "Contents" and open "Info.plist". Edit "Info.plist" to contain this xml code within the outer dict element. For an example, download the app bundle above.
<key>CFBundleIdentifier</key>
        <string>nl.pepijndevos.urlhandler</string>
        <key>CFBundleURLTypes</key>
        <array>
                <dict>
                        <key>CFBundleURLName</key>
                        <string>Applescript urls</string>
                        <key>CFBundleURLSchemes</key>
                        <array>
                                <string>http</string>
                                <string>https</string>
                                <string>file</string>
                        </array>
                        <key>LSIsAppleDefaultForScheme</key>
                        <true/>
                </dict>
        </array>
Now, when you click an url it will just open your browser. I'm not sure what to do exactly, but here is what I did
  1. Run the app once
  2. Set it as the default browser in Safari
[update] Using Linux? Try this script on Humbug.in This script has also appeared on lifehacker.com download.com and maxosxhints.com