Thursday, May 20, 2010

Smart(er) browser chooser

My problem is this:
I use multiple browsers. Firefox for the plugins; Chromium for the speed; Chrome for when Chromium breaks :) ; Opera for.... whatever.
Most desktop managers (I use Gnome) let you define your default browser. And most apps use that configuration when you click on a link in them.
The thing is that sometimes you already have a browser opened, and it's not your default browser.
For instants, sometimes I have Chromium running but my default browser is firefox.
If I click a link on the desktop or some app, I don't want it to launch firefox (with the "hundreds" of tabs I have opened in it's session)

So this is my solution. Place this code as an executable in /usr/bin/browser and configure Gnome to use it as the default browser.
#!/bin/bash
# The last option should be the default. To be used if none are running
BROWSERS=( firefox chromium-browser opera google-chrome x-www-browser )
for browser in ${BROWSERS[@]}; do
RUNNING=$(pgrep -u $USER -f $browser);
if [ "$RUNNING" ]; then
break
fi
done
COMMAND=$(type -P $browser);
exec $COMMAND "$@"

It goes through the list of browsers and if any of it's running, opens the clicked link in that one. If none are opened it launches the last one on the list.

1 comments: