Single-Instance Chromium App Shortcuts in Linux

Update (Sep 2022): As this issue mostly affects Chrome Shortcuts/Apps, the Clutter Free Chrome extension is a significanlty easier way to prevent duplicate app instances.

After recently installing Linux Mint 19.3 Cinnamon I started my usual creation of Chromium Application Shortcuts to the dozen or so webapps I use frequently. Gmail, YouTube, Google Calendar, Reddit, my local unRAID servers etc. I like having these sites as separate windows and icons in the task switcher (and in Plank).

Task Switcher

One problem I ran into is everytime I re-launched a Chromium shortcut it opened a new window. Sometimes I wouldn’t even notice until later using the App switcher and suddenly see 3 Gmail icons.

I really hoped that there would be a chromium command line switch that would tell the app to re-use an existing window, but I was unable to find one. Instead I ended up modifying the .desktop launcher file for each Chromium shortcut to run a small script that checks if the instance is running already and use that, otherwise launch a new window.

Here is the script to see if a Chromium instance is already running. It will get executed via the .desktop launcher file with the unique app_id as the first argument.

#! /bin/bash

if wmctrl -xl | grep "${wm_class_id}" > /dev/null ; then
# Running. Bring to front.
wmctrl -x -R "${wm_class_id}"
# Not Running. Open new window.
/usr/bin/chromium-browser --profile-directory=Default --app-id=${app_id}

I put the above file into /usr/local/bin/

Next we need to modify the .desktop launcher files to run this script instead of just launching a new Chromium window each time.

Change to the ~/.local/share/applications directory. Each of the chrome-*-Default.desktop files are a launcher for one of your Chromium shortcuts. Within each of these files is a line that looks like:

Exec=/usr/bin/chromium-browser --profile-directory=Default --app-id=[UNIQUE_APP_ID]

This needs to be changed in each file to instead read:

Exec=/usr/local/bin/ [UNIQUE_APP_ID]

If you only have one or two to change, you may as well do it manually. If, like me, you instead have a dozen or more to change, here is a short sed command to quickly rewrite the necessary line in all the files in one go.

sed -i 's:/usr/bin/chromium-browser --profile-directory=Default --app-id=:/usr/local/bin/ :' ~/.local/share/applications/chrome*.desktop

Now when you go to launch one of your Chromium Application Shortcuts, you should find you’ll only ever have a single instance of each shortcut type running at one time.