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.

17 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?

Vote539 said...

Thanks for the tip! This works great for me in OS X 10.7.

Simon Katz said...

Thanks for this script — it solves an annoying problem. Why Google can't get this right is beyond me.

The script works for me on 10.7.5, but not on my new machine with 10.8.4.

On 10.8.4, it's ok for opening docs from the Finder or when I use Alfred's Google search function (http://www.alfredapp.com/), but using the Unix "open" command doesn't work.

It turns out that using the Unix "open" command invokes a different handler on 10.8.4 — "on run" instead of "on open url". See http://www.blogger.com/comment.g?blogID=1000315451233282197&postID=2103132787259983282&page=1&token=1377948616586 for more detail.

It would be great if someone could find a fix.

Simon Katz said...

Sorry, wrong URL in my previous comment. It should be http://macscripter.net/viewtopic.php?pid=166461#p166461

Mike Eng said...

Looks like a broken link to the script bundle. http://screencast.com/t/010joFrRu5

Fastsquatch said...

@Mike Eng - you are correct as entropy struck me again - be on the lookout for a post shortly entitled "'yum update' on a stock RHEL Amazon Web Services Free Tier server won't boot'

I just moved my stuff to the cloud and silly me for thinking yum update was safe. AWS runs custom kernels to boot off their cloud storage, and yum update pulls in a stock kernel. Hilarity ensues. So my servers were down.

I just revived them, and the download link works again

Possibly worth noting the script continues to work like the champion hack it is after an upgrade by me to Mavericks over the weekend.

Cheers!

Simon Katz said...

I commented before that the script didn't work for me on 10.8.4.

Just wanted to say that all is ok again now I've upgraded to Mavericks 10.9.1.

Fastsquatch said...

@Simon Katz - glad to hear a second success report on Mavericks. I can't believe this silly script is necessary but I'd go crazy without it I think so I'll keep it updated and working... Cheers

Frank Webb said...

Any way to open this in Incognito mode? I can't seem to find a way to tell Chrome by Applescript to open anything in Incognito. It's easy to launch Chrome in Incognito just by --args --incognito in the first place, but if it's already running, how do I open anything new in Incognito? I would like to have this script open an URL form Safari's Develop menu in Chrome Incognito. Thanks for help.

lithboy said...

As of OS X Yosemite, there's no longer an ability to select an arbitrary application as your default browser. Safari only lets you choose Safari or Chrome, not this helper app.

Fastsquatch said...

Hi lithboy - I've had success on Yosemite (I just moved to it a couple days ago and tried this actually) by using the "Default Apps" preference pane. You can set it by going in there and altering the behavior for http and https protocols, and it seems to stick for me? RCDefaultApp is the official name, from the wonderful folks at Rubicode: http://www.rubicode.com/Software/RCDefaultApp/

Hope this helps, would love to hear if it succeeds