Setting web browser script
How do I set the default browser?
How often have we heard the question?
I just came across an issue where my development environment did not open the expected browser.
This occurs when the mime settings is out of sync with each other. To get my system to behave I put together this tiny script.
Create the file and make it executable (you may need to create the bin folder beforehand)
mkdir -p ~/.local/bin
touch ~/.local/bin/set-browser.sh && chmod +x ~/.local/bin/set-browser.sh
Then edit the file with your favorite editor and copy-paste below into the file and save it.
#!/bin/bash
#
# Setting default browser using xdg utils
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# 2021-11-20
# @linux-aarhus (c) root.nix.dk
# https://root.nix.dk/en/utility-scripts/set-browser-script
#
print_usage(){
echo "Usage:"
echo " Supply browser desktop file as the first argument"
echo " e.g. set-browser.sh firefox.desktop"
echo ""
}
show_available_browsers(){
echo "Available browsers (installed)"
echo "---"
# funny thing with cut when splitting a path
# one would expect the column to be number 4 but it is 5
# as string starts with `/` and therefore first column is empty
# e.g. /usr/share/applications/firefox.desktop
grep -rl 'internet' /usr/share/applications | cut -d'/' -f5
echo "---"
}
set_browser() {
# set scheme handlers
xdg-mime default "$1" x-scheme-handler/https
xdg-mime default "$1" x-scheme-handler/http
# set default browser
xdg-settings set default-web-browser "$1"
echo "Default browser set using $1"
}
# check if argument is supplied
if [[ "$1" == "" ]]; then
print_usage
show_available_browsers
exit 1
fi
# check if desktop file
if [[ "$1" =~ ".desktop" ]]; then
# check if the file exist
if [[ -f "/usr/share/applications/$1" ]]; then
set_browser "$1"
else
echo "$1 - file not found"
show_available_browsers
exit 1
fi
else
echo "$1 is not a .desktop file"
show_available_browsers
exit 1
fi
The script requires you to know the desktop file name for the browser in question - run the script without argument to get a list of available browser launchers.
You can find the correct name by searching the folder /usr/share/applications
as this example shows.
The command searches the file contents for the string internet which indicates a browser applications, pipes the result through cut and print only the last column. Then use the appropriate file as argument for the script.
$ grep -rl 'internet' /usr/share/applications | cut -d'/' -f5
org.midori_browser.Midori.desktop
microsoft-edge-dev.desktop
firefox-developer-edition.desktop
vivaldi-stable.desktop
firefox.desktop
To set the browser run the script like this
set-browser.sh microsoft-edge-dev.desktop
Source: Set Browser Script | root.nix.dk