Safari MacOS – Make Safari Open Search Results in New Tab by Default

macossafarisearch

I'd like for results from the search toolbar to open in a new tab by default, like I've configured in Firefox. How can I get this same default behavior in Safari? I already know that Command-Enter will do this for me, but I want to change the default behavior so that I don't have to hold down Command every time I do a search.

Best Answer

If you're in to some work, then Glims with a custom search provider, and some AppleScript to create your own protocol handler (URL scheme) can do the trick. But I think pressing Command-Return is much easier. ;-)

Here's what you'd need:

  1. Open Applications, AppleScript Editor, and paste the following:

    -- See http://superuser.com/questions/204435/
    on open location fullUrl
      -- fullUrl includes the URL scheme, like "newwindow:" or "newwindow://"
      set a to the offset of ":" in fullUrl
      set b to the offset of "//" in fullUrl
      if b = a + 1 then set a to a + 2
      set theUrl to text from (a + 1) to -1 of fullUrl
      -- Delegate the new URL to whatever is the default handler:
      tell application "System Events"
        open location theUrl
      end tell
    end open location
    
    -- Just in case this is invoked directly from Finder:
    set choice to button returned of (display dialog "Please use a URL like
        newwindow://http://google.com
    to use this." buttons {"More info...", "Cancel"})
    if choice = "More info..." then
      tell application "System Events"
        open location "http://superuser.com/questions/204435/"
      end tell
    end if
    
  2. Select menu File, Save As, and be sure to select File Format: "Application". This will create something that looks like a single application, but actually holds a folder structure.

  3. In Finder, find the place where you saved the application, right-click it and select "Show package contents".

  4. Find file Contents/FileInfo.plist and open it with a text editor.

  5. At the end, just above the last two lines </dict></plist>, add:

    <key>CFBundleIdentifier</key>
    <string>com.superuser.204435.NewWindow</string>
    <key>CFBundleURLTypes</key>
    <array>
      <dict>
        <key>CFBundleURLName</key>
        <string>NewWindow</string>
        <key>CFBundleURLSchemes</key>
        <array>
          <string>newwindow</string>
        </array>
      </dict>
    </array>
    
  6. Move the whole application package into, for example, Application/Utilities (to ensure Launch Services registers it).

  7. Test in any browser, using newwindow://http://www.google.com/search?q=abc. If it doesn't work, then double-click the application or even rename the package, just to trigger the discovery by Launch Services again.

  8. Install the Glims plugin for Safari.

  9. In Safari's preferences, tab sheet Glims, Search Engine List, add a new entry. Note that Glims requires the slashes after newwindow:. Without that, Glims would prefix http:// to whatever text you entered:

    newwindow://http://google.com/search?q=#query#
    

I don't know how to change the icon that Glims shows... Also, when using this, there's no key you can hold down to not open the results in a new window.

Related Question