Make Safari open links from applications in the current space

Oct 28, '09 07:30:04AM

Contributed by: jaysoffian

Safari is my default browser, and I have its preferences set to open links from applications in a new tab in the current window. In addition, I use Spaces to segregate my tasks, and I have the 'When switching to an application, switch to a Space with open windows for the application' Spaces' preference unselected.

It is often the case that I have a Safari window open in Space 1 (say), but I've switched to Space 2 to work on another task. I then need to open a URL from an application other than Safari while working on the Space 2 task. If Safari doesn't already have a window open in Space 2, but does have a window in Space 1, Safari opens the URL in a new tab in the window in Space 1. I would rather Safari open a new window in the current Space to handle the URL.

This is doable, but requires a little magic.

As a prerequisite, you'll need to install the RCDefaultApp preference pane.

  1. Open AppleScript Editor and paste in the following code:
    on open location theURL
      tell application "System Events"
        if (count of windows of process "Safari") = 0 then tell application "Safari" to make new document
      end tell
      tell application "Safari" to open location theURL
    end open location
  2. Save the document using file format Application, and select the Stay Open checkbox. Name it SafariURLHelper. I have an Applications folder in my home directory in which I saved it.
  3. Using the Finder, navigate to where you saved SafariURLHelper, then Control-click its icon and select Show Package Contents from the contextual menu.
  4. Inside the Contents folder, find Info.plist, and open it in a text editor.
  5. Add the following snippet to the bottom of the file, just before the closing </dict> and </plist> tags:
    <key>CFBundleIdentifier</key>
      <string>com.apple.AppleScript.SafariURLHelper</string>
      <key>CFBundleURLTypes</key>
      <array>
        <dict>
          <key>CFBundleURLName</key>
          <string>SafariURLHelper</string>
          <key>CFBundleURLSchemes</key>
          <array>
            <string>http</string>
            <string>https</string>
          </array>
        </dict>
      </array>
      <key>NSUIElement</key>
      <true/>
    A little bit of explanation. Most of the above text comes from this tip on macosxautomation.com, and just registers this application as something which can handle http and https links. The NSUIElement addition causes SafariURLHelper to be a background (faceless) application.
  6. Save and close Info.plist, then in the Finder, double-click on SafariURLHelper. Nothing will appear to happen. This is fine; you've just launched SafariURLHelper as a hidden background process. More importantly, this registers it as a handler for http and https URLs.
  7. Open the Default Apps System Preferences pane. Click on the URLs tab, then set the default application for both http and https to SafariURLHelper (it should appear in the drop-down automatically).
That's it. Now http and https URLs will be sent to SafariURLHelper. It will check to see if Safari has any windows open in the current space, and if not, it will tell Safari to open one. It then just passes the link along to Safari.

Comments (10)


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