Opening desktop links on Linux with different browsers and profiles

Published on at techie , 3 mins.

This is a quick note on the how I fixed something that has been bothering me for quite a long time but never took the time to fix. See, I use browser profiles to separate work and personal contexts (bookmarks, themes, etc). The issue is that only one of them is a default for clicking on links on any desktop application.

I have my work profile as the default because most of my browser external clicks were coming from Slack or the terminal so when a link was meant to be open in my personal profile I would copy the link and paste it directly in the address bar.

On the other hand, I’ve recently moved to Thunderbird, a desktop mail client, unifying both personal and work accounts. So now my “copy & paste in personal browser” occurrence escalated quite a lot and got to a point that was very annoying.

To fix this I found a StackExchange post that was addressing exactly this issue. There was a comment with a slightly better solution so that is what I ended up implementing.

The idea is to create a small script and register it as the default desktop browser. This script will trigger a simple interface that asks on which browser you want your link to be opened.

The script, placed on any other folder of my $PATH:

#!/bin/sh

BROWSER=$(zenity --info --title='Open With' \
        --text="Select the browser" \
        --ok-label="Cancel" \
        --extra-button="firefox -P work" \
        --extra-button="firefox -P personal" \
        --extra-button="brave-browser")

$BROWSER $* &

It is quite straight forward to follow. It uses zenity to show a simple window that offers three options to open two different Firefox profiles and Brave Browser. Then executes the selected option along with the URL that will be passed as a parameter.

Then I created a desktop file at ~/.local/share/applications/select-browser.desktop

[Desktop Entry]
Version=1.1
Type=Application
Name=Select browser
Icon=firefox
Exec=[[path to select-browser script]] %U
Actions=new-window;new-private-window;NewShortcut;NewShortcut1;
Categories=Network;WebBrowser;
StartupNotify=true
StartupWMClass=select-browser
MimeType=x-scheme-handler/unknown;x-scheme-handler/about;text/html;text/xml;application/xhtml_xml;image/webp;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;

And finally I ran

$ xdg-settings set default-web-browser select-browser.desktop

That’s it. Now when I click on any link I get this screen that just adds a quick click to my workflow.

Open with dialog
Open with dialog

What do you think? I’m alone on this annoyance on these days where everything happens in the browser and we don’t use desktop applications that much anymore?

2023-12-05 update

A simple improvement I did to the script was to put my own labels on the buttons and then use switch to assign the appropriate system command:

#!/bin/sh

SELECTION=$(zenity --info --title='Open With' \
        --text="Select the browser" \
        --ok-label="Cancel" \
        --extra-button="📊 ELASTIC" \
        --extra-button="🏠 PERSONAL" \
        --extra-button="🦁 BRAVE")

case $SELECTION in

  "📊 ELASTIC")
        BROWSER="firefox -P elastic"
        ;;
  "🏠 PERSONAL")
        BROWSER="firefox -P personal"
        ;;
  "🦁 BRAVE")
        BROWSER="brave-browser"
    ;;

  *)
    firefox
    ;;
esac

$BROWSER $* &

Open with dialog, version 2
Open with dialog, version 2

I also took today the opportunity to upload the script to my small repository of utilities and configurations so it is easy to set up in future workstations.