How to "run or raise" applications on Gnome
In my daily workflow, I rely heavily on specific apps like the terminal and calculator. To access them quickly, I have configured keyboard shortcuts.
The problem? By default, every time I hit the shortcut, a new instance of the program launches. This quickly leads to desktop clutter. It is much preferable that if an instance already exists, it is brought to the foreground (raised) instead of launching a duplicate.
I found a solution for Gnome that achieves exactly this. It utilizes the extension Activate Window by Title.
This extension focuses an application window based on its title or window class and can be controlled via the command line. By writing a shell script and invoking it via a keyboard shortcut, we can use the extension to raise an existing instance.
For each application I need to access quickly, I defined a script named runorraise-<application>.sh in ~/.local/bin (ensure this location is in your $PATH). The script launches a new instance if the application is not running; otherwise, it calls the extension to raise the existing window.
Here is the script for the Terminal:
#!/bin/bash
# Check if the process is NOT running
if ! pidof gnome-terminal-server > /dev/null; then
# Launch a new instance
gnome-terminal
else
# App is already running, so focus the existing instance
busctl --user call \
org.gnome.Shell \
/de/lucaswerkmeister/ActivateWindowByTitle \
de.lucaswerkmeister.ActivateWindowByTitle \
activateByWmClass \
s 'gnome-terminal-server'
fi
Similarly, the script for the file manager (here Gnomes Nautilus) is the following:
#!/bin/bash
if ! pidof nautilus > /dev/null; then
nautilus
else
busctl --user call \
org.gnome.Shell \
/de/lucaswerkmeister/ActivateWindowByTitle \
de.lucaswerkmeister.ActivateWindowByTitle \
activateByWmClass \
s 'org.gnome.Nautilus'
fi
Browser (here Firefox):
#!/bin/bash
if ! pidof firefox > /dev/null; then
firefox
else
busctl --user call \
org.gnome.Shell \
/de/lucaswerkmeister/ActivateWindowByTitle \
de.lucaswerkmeister.ActivateWindowByTitle \
activateByWmClass \
s 'firefox'
fi
Calculator:
#!/bin/bash
if ! pidof gnome-calculator > /dev/null; then
gnome-calculator
else
busctl --user call \
org.gnome.Shell \
/de/lucaswerkmeister/ActivateWindowByTitle \
de.lucaswerkmeister.ActivateWindowByTitle \
activateBySubstring \
s 'Calculator'
fi
Finally, map the scripts to your keys by navigating to Gnome Settings > Keyboard > Custom Shortcuts and creating a new shortcut for each script.