Open external links in currently running browser

May 07, '10 07:30:01AM

Contributed by: pepijndevos

If you are using multiple browsers, and are annoyed by Safari (or your designated preferred browser) popping up while using a different browser, this hint can remove that annoyance.

I'm a web developer, and while my main browser is WebKit, I also have Opera, Chrome and Firefox installed. If I'm using Opera and click a link in Mail, that link will open in Safari. So I wind up with two running browsers, or having to copy the URL over to Firefox manually. Not cool.

I have figured out a way to make an AppleScript 'proxy' app that delegates URLs to the browser that is currently running, no matter which is set as the default. Paste the following code into AppleScript Editor:

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
Modify the above as needed to reflect your own suite of browsers, then save this script as an application bundle somewhere. Next, you need modify a bit of the application bundle you just created.

Drill down into the app by Control-clicking on it, and choosing Show Package Contents from the pop-up menu. Navigate into Contents and open Info.plist in your favorite text editor, and paste this code near the end:

<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 run the application -- nothing will happen, but this will register the app with Launch Services.

The only thing that remains to be done now is open Safari, go to the General tab in its Preferences, and set your new AppleScript as the default browser.

[crarko adds: This hint originally appeared on the author's blog. You can also download a final version of this app, either from the linked blog entry, or mirrored here on Mac OS X Hints [28KB]. If you download the application, note that you'll need to dig into the package and modify the source script to reflect the browsers you use. I tested this one, and it worked as described.]

Comments (15)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100503134708927