Some of the shell code from that source doesn't do what it claims to do; mainly the type that goes:
if [ -n $(pgrep -f vlc) ]; then (vlc &); fi
This runs vlc &
in two cases. If there are no processes matching "vlc", and if there are exactly one process matching "vlc". If there are more than one process matching "vlc", vlc &
will not be run. So these if blocks are rather useless. Just running vlc &
instead will likely be more correct.
Bad shell advice is unfortunately commonplace on the internet.
From your problem description, what you want the result to be is that the program is run if it isn't already running, and if it is running, bring that window to the front, and give it focus. To do this, wmctrl
could be used.
wmctrl -a 'Mozilla Firefox'
will find a window containing "Mozilla Firefox" in its title, and "activate" it. That is, move to the workspace it's in, bring it to the front and give it focus. Secondly, if no windows match, wmctrl will do nothing and exit with a non-zero (false) exit status, in which case we can assume the program is not running, and start it instead.
wmctrl -a 'Mozilla Firefox' || firefox &
It's still not perfect though. Other windows than firefox windows could potentially contain that string in their title, so we should find a more reliable way of identifying the right window in this case.
With -x, wmctrl will operate on the VM_CLASS of a window, which will typically have a unique value per program. All firefox windows will have VM_CLASS "Navigator.Firefox" as can be seen by running wmctrl -lx
while firefox is running
$ wmctrl -lx
...
0x03ba3d43 3 gnome-terminal.Gnome-terminal pilot Terminal
0x04c000bc 0 Navigator.Firefox pilot Group #1 - Speed Dial - Mozilla Firefox
With this we can select the window that has exactly "Navigator.Firefox" as VM_CLASS
wmctrl -Fxa Navigator.Firefox || firefox &
The same should hopefully be applicable for the other programs.
Best Answer
What's completely undocumented is that Firefox expects a
--url
parameter and will launch a new window otherwise. If you don't want the new tab to navigate to a url, giveabout:newtab
as the url parameter.