Skip to content

Instantly share code, notes, and snippets.

@cesarolea
Created February 7, 2020 22:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cesarolea/49c55749b48464322ef9475aa4e65a4a to your computer and use it in GitHub Desktop.
Save cesarolea/49c55749b48464322ef9475aa4e65a4a to your computer and use it in GitHub Desktop.
Open browser based off of certain rules (like choosy for macOS)
#!/bin/bash
URL=$1
DOMAIN=$(echo "$URL" | awk -F[/:] '{print $4}')
## Domain starts with
if [[ $DOMAIN =~ $(echo ^\($(paste -sd'|' /home/your-user/.config/url-start.txt)\)$) ]]; then
chromium-browser "$URL" & disown
elif [[ $DOMAIN =~ $(echo ^\($(paste -sd'|' /home/your-user/.config/url-is.txt)\)$) ]]; then
firefox "$URL" & disown
else
firefox "$URL" & disown
fi
[Desktop Entry]
Name=Choosy
GenericName=Browser Chooser
Comment=Choose what browser to use for specific links
Categories=Network;
Exec=choosy %u
Type=Application
MimeType=x-scheme-handler/unknown;x-scheme-handler/about;text/html;text/xml;application/xhtml_xml;x-scheme-handler/http;x-scheme-handler/https;
https://www.mozilla.org
drive.google.com
docs.google.com
meet.google.com
app.gotomeeting.com
hangouts.google.com
@cesarolea
Copy link
Author

This is a very simple way of setting a different default browser, based off of certain rules. A poor man's version of https://www.choosyosx.com/ which is one of the utilities I missed the most when switching from macOS to Linux. I use Firefox as my main browser, but like to use Chromium for certain websites that work better with Chromium. With this setup if for example I click a Gotomeeting link in an email, it will automatically open in Chromium instead of Firefox.

Installation

First, edit choosy and replace your-user with your actual username. The one included here uses Chromium as an alternate browser, but it can be anything as long as you can pass a URL as argument. Copy choosy to your path and make it executable:

cp choosy ~/bin/
chmod +x ~/bin/choosy

Copy choosy.desktop to either ~/.local/share/applications/ or /usr/share/applications/

cp choosy.desktop /usr/share/applications

Log out of your session, and log back in. Your desktop environment should offer Choosy as a default browser option. For example in Gnome it will be listed under Settings - Details - Default Applications - Web.

Finally, edit url-start.txt and url-is.txt to your liking:

  • url-start.txt contains a list of URLs that will match on the start of the URL. For example https://drive.google.com/drive/search?q=whatever will match due to the first entry drive.google.com. Protocol is ignored.
  • url-is.txt contains a list of URLs that will do exact matches only.

Copy both to your ~/.config folder:

cp url-* ~/.config

Uninstall

Simply choose a different default browser in your desktop environment and you should be good.

@Tuxliri
Copy link

Tuxliri commented Jan 5, 2023

Hi this is not working for me on Ubuntu 22.04, when I logout from my account and log back in there is no additional entry in gnome settings. I am using wayland, could this be the issue?

@cesarolea
Copy link
Author

I don't think wayland would interfere, but have no way of testing it myself. Verify that your choosy.desktop is located either in ~/.local/share/applications/ or /usr/share/applications/ (try the other location if the first doesn't work) and make sure permissions are right. Supposedly .desktop files don't require execute permissions but according to https://askubuntu.com/questions/419610/permission-of-a-desktop-file it is sometimes required for it to work properly. Finally I would try rebooting if simply logging out doesn't pick up the new browser option.

@Tuxliri
Copy link

Tuxliri commented Jan 6, 2023

I made sure to follow the instructions but no entry would show up in the default apps menu of Gnome. What worked for me was to execute xdg-mime default choosy.desktop x-scheme-handler/http && xdg-mime default choosy.desktop x-scheme-handler/https in the terminal to set it manually.

@cesarolea
Copy link
Author

I made sure to follow the instructions but no entry would show up in the default apps menu of Gnome. What worked for me was to execute xdg-mime default choosy.desktop x-scheme-handler/http && xdg-mime default choosy.desktop x-scheme-handler/https in the terminal to set it manually.

Glad you could make it work. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment