Ubuntu – Set default browser to OPEN browser

browserdefault-browser

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

Best Answer

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.