4

I am switching constantly between browsers (three different at the moment). So i was also switching default browsers to open my mail / new links etc...

I also saw that with the command sudo update-alternatives --config x-www-browser the browsers have different priorities.

So I had the idea that it could be possible that the default browser could be the browser/s that is/are actually open and running! And if two are running, the one with higher priority is chosen. Unluckily I did not find anything.. Is this possible? Would be really great!

cheers dmeu

4
  • So if you happen to be running firefox, then all links would open in firefox, but if you happen to running Chromium, all links would use that, and if you're running both, it would use priorities to determine which one opens the links? Might be possible with some manipulation of the /bin files that start each browser, but I'm afraid it's beyond my ability.
    – Scaine
    Jan 8, 2011 at 12:41
  • 1
    +1 for nice idea. Never thought of it, but it can be useful ^^
    – Lekensteyn
    Jan 8, 2011 at 15:21
  • 1
    thanks a lot for these great answers (learned some new stuff ;-)
    – dmeu
    Jan 10, 2011 at 8:09
  • Interestingly, it's also possible to do this on Mac OS X: superuser.com/questions/41878/… Jan 3, 2013 at 5:50

2 Answers 2

5

Here's something that works:

#!/usr/bin/env python

import sys, commands

browsers = [
    "firefox",
    "google-chrome",
    "chromium-browser",
    "epiphany-browser",
]

done = False
ps = list(i.split()[10] for i in commands.getoutput("ps aux").split("\n")[1:])
for i in browsers:
    if any(j.endswith(i) or j.endswith(i+"-bin") for j in ps) and not done:
        commands.getoutput(i + " " + (" ".join(sys.argv[1:]
            if len(sys.argv) - 1 else "")))
        done = True

if not done:
    commands.getoutput(browsers[0]["cmd"] + " " + " ".join(sys.argv[1:]))
  • Just re-order the list on top of the file any way you like, add and remove browsers. The command you enter will be whatever you would type in the command-line to start this browser.

  • save this script somewhere, right click on it, go to properties → permissions and set executable. I recommend saving it in /home/<username>/.browser.

  • Find the file (hit Ctrl+H to show hidden files), right click and select Properties, then go to Permissions and set Allow executing file as program.

  • Go to System → Preferences → Preferred Applications, select Custom and type /home/<username>/.browser into the Command field.

The first browser that appears in the list and is running will be chosen to open the web site. If none of them is running, the first one will be opened.

I've tested this and it seems to work rather well, none the less, let me know if there are any errors in it. And it'd be cool if there was a method to do this that doesn't involve programming, this is just my peculiar solution.

2
  • works perfectly thanks! should be included in the ubuntu distribution ;-)
    – dmeu
    Jan 10, 2011 at 17:54
  • @Stefano, How is the last step setup for Ubuntu 15+ with Unity?
    – user10853
    May 15, 2016 at 2:16
3

What a great idea!

I immediately set this up and it works great on my system. Adapt the following script to your needs (you'll need superuser rights to install the script!):

#!/bin/bash

# looking for Firefox instances
pgrep -l '^firefox-bin$' > /dev/null

if [ $? -eq 0 ]; then
    echo
    echo "  Opening link in Firefox..."
    echo
    /usr/bin/firefox "$1"
    exit
fi

# looking for Konqueror instances
pgrep -l '^konqueror$' > /dev/null

if [ $? -eq 0 ]; then
    echo
    echo "  Opening link in Konqueror..."
    qdbus `qdbus | grep -m1 konqueror` /konqueror/MainWindow_1 org.kde.Konqueror.MainWindow.newTab "$1" False
    exit
fi

# no browser instances found, so open default browser
echo
echo -n "  No open browser found.  "

if [ -z "$1" ]; then
    echo "Opening new Firefox instance..."
    echo
    /usr/bin/firefox &
    exit
else
    echo "Opening link in new Firefox instance..."
    echo
    /usr/bin/firefox "$1" &
    exit
fi

The unmodified script will try to open a link (specified in $1) using Firefox. If no instance of Firefox is found, it will try to reuse Konqueror. Finally, if no instance of Konqueror is found, the script will open a new instance of Firefox.

When you're done, save the script at /usr/local/bin/reuse_open_browser and run the following commands:

sudo chmod +x /usr/local/bin/reuse_open_browser
sudo chown root:root /usr/local/bin/reuse_open_browser

sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser /usr/local/bin/reuse_open_browser 100

Then select the script as default by running

sudo update-alternatives --config x-www-browser

Enjoy! :)

1
  • 1
    You can get rid of needing superuser privileges by just saving it to your home directory, possibly .hidden. Setting the default web browser via Preferred Applications is per-user. Jan 8, 2011 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .