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.

30 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.

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

lithboy said...

Fastsquatch, it worked, thanks!

Alan Porter said...

I just recently upgraded to Yosemite (OS X 10.10), and I was having trouble setting the default browser to OpenURLInNewChromeWindow. I used RCDefaultApp, but on the "browser" tab, every time I tried to set another browser, the pull-down menu would snap back to whatever the current setting was.

I ended up going to the "apps" tab, and from there I could choose the new browser and check the HTTP and HTTPS checkboxes and checking "set as default". That worked.

I just wanted to pass along the tip, in case others are trying to set their default using RCDefaultApp.

Alan

Mike Eng said...

Thanks for this contribution, however, when I try to open the script, I am getting an error: "OpenURLInNewChromeWindow" is damaged and can't be opened. You should move it to the Trash. http://screencast.com/t/CCvuFjtnJz

on Mac OS 10.9.5

Fastsquatch said...

Hey Mike - I think this is related to the .app bundle not being signed. It appears you have two options assuming that is the case - either go to security preferences and allow apps from anywhere (this should work but I'll quickly note it is potentially less safe) or specifically allow that app using a Terminal command similar to the one listed here: http://apple.stackexchange.com/questions/58050/damaged-and-cant-be-open-app-error-message

I hope this helps! If it doesn't or anyone knows a more elegant way, please enlighten us.

Mike Eng said...

@Fatsquatch

That worked like a charm. Thanks!

Darren Cook said...

The last comment to use xattr -rc [path to script] did fix the "damaged" error message. However when I double-click it it launches and closes, but does not register with safari. This is on 10.10.x....both Safari > Preferences General and System Preferences > General > Default Web Browser have locked down app selection, so you can only use apps that register as a browser, apparently. My Sys Prefs > General > Default Web Browser show Safari, Chrome, Firefox, Evernote, And VLC, but not the applescript. Any ideas how to solve this one?

Fastsquatch said...

Hey Darren - It looks using the "RCDefaultApp" 3rd Party preference pane will allow you to manage default apps including allowing you to specify an "Other..." app. I believe that's what I've used to set it - http://www.rubicode.com/Software/RCDefaultApp/ - hope that helps!

Darren Cook said...

Almost...I installed the pane, but when I choose the applescript it flips right back to the system default. The pane hasn't been updated since 2009 so I don't have much hope it will be fixed.

I've sent an email to the developer of OnyX to see if he can expose the default browser preference there.

Fastsquatch said...

Dang - I wonder if this just happened - I've noticed since the most recent OS X update (I received one just a few days ago) I've had to reset security preferences a lot, perhaps something about the security model (and what apps to trust) has changed fundamentally, while leaving previous preference changes intact. Any more information you dig up would be greatly appreciated - Hopefully you figure something out - this script probably executes 100 times a day for me, would be very sad if it stopped working!

Darren Cook said...

This has been bugging me for a while under 10.10 and now 10.11 El Capitan. In particular because Chrome defaults to Add New Tab behavior, which it needs an existing window for, so if you're using desktop switching it will switch to the last used desktop with a Chrome window; very annoying.

I did some research based on the thought that apps must be able to register for the System Preferences > General > Default web browser dropdown. I didn't find out how to register the applescript app for this dropdown (there is probably a way) before I found this instead: an app to let you register custom url schemes.
https://onflapp.wordpress.com/lincastor/
It turns out this app can also modify the defaults for http and https!

For our particular purposes (Chrome in a new window), here are the steps:
1. Download Lincastor, throw it in your app directory, right-click > Open to launch it. Click the Setup button.
2. Click the "add new scheme" button.
title: OpenHTTPInNewWindow
scheme: http
handler: AppleScript
3. Here's the Applescript to paste in, based on this thread:
on handle_url(args)
tell application "/Applications/Google Chrome.app"
make new window
activate
set URL of active tab of first window to |URL| of args
end tell
return 1
end handle_url
4. Repeating step 2 but for https:
Click the "add new scheme" button again.
title: OpenHTTPSInNewWindow
scheme: https
handler: AppleScript
Paste in the same AppleScript from step 3
5. Click Save and Activate.
OSX Should pop up a dialog after a few seconds about your default browser changing, click 'Use LinCastor'

Tada! Links now open in a new Chrome window on your current desktop. At least as far as I have tested it (clicking links from Mail).

Here are some more notes for those interested:
- The "default web browser" setting is stored in ~/library/preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist . (If you have XCode installed, double-clicking this file from the finder opens it nicely.)
- I previously had Chrome as my default browser, and saw it assigned to a few entries in the plist file. After the above setup, LinCastor became the registered app for the http and https entries, but not the others. So there may be situations where Chrome is still the default.
- System Preferences doesn't register the change: it still shows Chrome as my default. This seems fine as long as you don't change it (which will replace the entries in the plist file, causing you to need to open LinCastor again and click Save and Activate to fix it.)
- FYI the google search that led me to find this solution was "OSX change the URL handler for http: in Launch Services"

Hope this helps some other folks!

Fastsquatch said...

Darren - that looks pretty complicated (and the original solution is still working for me? But I installed it early in the 10.x series...) but if this works for other people great! I can see how it would work, sure. Thanks for sharing

Justin Rolfe-Redding said...

Thanks, Darren! Used the script with Lincastor Browser (seemingly a newer app from the same developer as Lincastor, but available on the Apple Mac App Store, and featuring some additional useful features, like picking between browsers for each link clicked).