LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices

Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Adaptive "default" browser script (rudimentary, but seems to work)

Posted 06-28-2014 at 03:22 AM by the dsc
Updated 07-01-2014 at 04:45 PM by the dsc ("bugfix")

Sometimes you open an application that will in turn open some web page, and it will automatically open it with the default browser. The problem is that often you'd prefer that it had been opened in whatever other browser that may be already running, even if it's not the system's default. So I came up with this script in order to try to get this behavior:

Code:
#!/bin/bash
# This "software" (so to speak) is totally UNLICENSED and barely documented. What you see is all there is. Run at your own risk.

browsers="favorite secondbest thirdone thatsok meh whatever still-better-than-starting-a-new-process"   

# list the browsers' executables in the variable above, separated with spaces
# they need to be also the "pgrepped" process name, which unfortunately differs in some cases, like chrome/google-chrome. 
# But there are workarounds, such as creating a "chrome" symlink pointing to "google-chrome", 
# or, for a more general solution, perhaps trying to replace the pgrep for something like:
#  ps -A | grep "$(echo $browsers | sed 's|\ |\\\||g')"    # perhaps "grep -x" ?

for i in `echo $browsers` ; do pgrep $i && break || i="" ; done    

# loop to check if there's a running instance of one of the browsers, in sequence. If there's
# one, it exits the loop, and "i" gets the browser name; if there's no browser running, "i" is emptied.


if [ ! -z "$i" ] ; then :    

# if the "i", running browser, is NOT empty, then does nothing, ":". The idea is that there would be a single "run the browser" command 
# at the end, but I'm not sure it's really the most intelligible logic/"phrasing". 


else for i in `echo $browsers` ; do which $i && break || i="" ; done    

# "else", if there was no running instance of some browser, then runs a similar loop as the previous, 
# but checks for actual executable, rather than instance.



fi

[ ! -z "$i" ] && ( $i "$@" & exit 0 ) || ( echo "Browsers listed in the $0 script are either not installed, misspelled, or unavailable for some mysterious reason." & exit 1 )
    

# at this point I think it's very likely that there's a browser executable on "i", so it's run with whatever parameters were passed along I guess. And I think it exits "successfully". 
# But perhaps it should be something with % rather than $, I don't know
# if "i" is still empty, gives the error message and exits "with failure"

As far as I've tested, which wasn't much, it will run a new instance of one of the browsers listed on the "browsers" variable, and use the most "favorite" from the running ones, according to the listing order, if there are more than one browser running. If there are no browser running, it will start a new instance of the first listed, if it's installed, and not misspelled on the variable.

In order to work, the script has to be set as the "default browser" for your desktop environment, which will vary. Most commonly I think it will be set as one or more symlinks on /etc/alternatives/, called "x-www-browser", "www-browser", and "gnome-www-browser". For KDE (or Openbox if you're exporting "de=kde") it will be set on ~/.kde/share/config/kdeglobals, on the "BrowserApplication[$e]=" variable/line. Which for me is set as "!x-www-browser". I don't know why there's the "!", it was there before, so I left it.

If there's anyone reading this who may try, including myself-from-the-future, I think worth thinking about possible issues with browser's "profiles" and more parameters that are not being taken in consideration in this script. E.g.: perhaps your default firefox/iceweasel is all bloated with extensions and you'd prefer to use a default/cleaner profile as the default browser.




Edit: Apparently you can't just delete/move the *-www-browser symlinks and replace with a symlink to the script, or whatever. It messes with upgrades of some browser packages, leaves them unconfigured. Just deleting the "hacked" symlink allows for a clean upgrade, though. The "proper" way to do this "hack" would be

# update-alternatives --install /usr/bin/x-www-browser x-www-browser /path/to/script/scriptname.sh 200

Then "update-alternatives --config x-www-browser", to choose the script.

The same for gnome-www-browser.


Edit 2: for "full" (I hope so) effect on Gnome-related apps, only gnome-www-browser won't do; you'd also need to create a "adaptive-defaultbrowser-script.desktop" (whatever - I edited a chromium.desktop and stripped all the "chromium" mentions and changed some "exec" line, saving with another name) and add it manually to the corresponding sections on mimeapps.list (location may vary I guess, and there are both user-specific and system-general settings). Or perhaps with xdg-mime, but I'm not sure.


https://wiki.archlinux.org/index.php...esktop_entries
Posted in Uncategorized
Views 191 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration