Sunday, June 26, 2011

Open links from external applications in a new window in Google Chrome on Mac OS X

I love tabbed browsing. I switched to Firefox way back in the day partially because Firefox allowed tabbed browsing. I recently switched to Google Chrome, which also has tabbed browsing.

I also love virtual desktops. I switched to Linux on the desktop way back in the day partially because it supported virtual desktops. I currently use Mac OS X which has "Spaces", so I continue to have virtual desktops.

However, when you combine the two, you potentially have a problem. If you have a web browser open, but it is not on your current virtual desktop, and you click on a link in a different application (a newsreader, perhaps), do you want that external link to open in a new tab in an existing browser window on a different space, or do you want it to open a new browser window on your current space, and open the link in a new tab on that new window?

Turns out lots of people prefer the second option - they want a new window on their current space with the link opened in it. Now, adults may disagree, and Firefox as well as Safari have a preference setting which allows you to change the behavior to either of those options. Everyone's happy.

Except Google Chrome users. Chrome always opens external links in the most recently used browser window, where ever it is.

This causes some people to be upset, example: http://www.google.com/support/forum/p/Chrome/thread?tid=1eb2eb870ba29e97&hl=en


I was also upset. I am a programmer though, and I can make the computer do what I want normally. So I made it do what I want.

1) Download this AppleScript bundle, and unpack it in ~/Library/Scripts/
2) Double-click it (to register it with Launch Services)
3) Open Safari and go to the General tab in the Preferences, and set your app as the default browser.
4) Share and enjoy

A fair bit of credit for making this work goes to Pepjin de Vos - http://pepijndevos.nl/2010/05/open-external-links-in-running-browser/ - I repurposed his idea/script to implement my workaround here.

7 comments:

Nelson said...

Thanks for this, it's working pretty well for me. The one drawback I've found is that windows always open on top, even in apps like Reeder that are doing their best to open the new window in the background.

Also, do you know if it's possible in AppleScript to have some logic where it will open in a tab in an existing window if the window is visible in the current space, otherwise make a new window?

Mike Hardy said...

Hi Nelson - glad this helped you.

I was unable to find a way to background the window after opening it - I'd like that feature as well. It may be possible, who knows

I did play with trying to get it to open a new tab the problem was that I couldn't correctly determine if there was a window on the space I was on or not. Eventually I gave up since everything I tried was unreliable, and even though I end up with many windows sometimes, at least it works every time. I'll take reliable but slightly annoying at least

Obviously feel free to tweak around there though - the applescript editor has a pretty good user interface into the library of available stuff, so you can try things quickly, maybe you can get either of those two things to work?

Cheers

Steven said...

One thing that was bothering me was this app continually appearing and disappearing in the dock every time I clicked a link.

The simple answer is to use the useful utility Dock Dodger to hide the app - this seems to speed it up a bit too (but perhaps its just a psychological effect).

Steven said...

In Safari, if you have a blank tab open, that will be used for opening a new link. I used this a lot if I wanted to open an external link into a specific group of tabs - switch to the window, open a blank tab, then click on the link in the external app (or use my Quicksilver shortcuts).

Here are the modifications to the AppleScript, if anyone else wants that functionality.

on open location theURL
tell application "/Applications/Google Chrome.app"
if number of windows is 0 or URL of active tab of first window is not "chrome://newtab/" then
make new window
activate
end if
set URL of active tab of first window to theURL
end tell
end open location

Mike Hardy said...

Brilliant - thanks Steven!

Steven said...

Note on the script I posted - the "activate" should come after the "end if".

Mike Hardy said...

I am extremely saddened to report that this is no longer reliably working as of OS X 10.7 I believe, and definitely not very well in 10.8. It works much of the time, but not all the time. Specifically, in NetNewsWire for instance, it no longer works well. Anyone know what changed or have a workaround?