2

What is really annoying in Ubuntu is that when some apps which don't need multiple instances such as 'appearance settings', a web browser, terminal window are loaded again when they are aready launched. For a browser or terminal I can still understand, although such apps have a 'new window' option. But for settings apps, multiple instances of e.g. 'Appearence' or 'Keyboard settings' are completely useless.

How do I prevent that the launcher launches an app (e.g. Firefox) again when it is already launched ?

I tried a shellscript called launch.sh:

#!/bin/sh
if ! pgrep $1 >/dev/null; then 
    "$2" &
fi

In the xfce launcher for Firefox I set

launch.sh firefox /usr/bin/firefox

which does work (firefox is not launched when it is areay open), but for xfce4-appearance-settings it does not work, because pgrep xfce4-appearance-settings returns nothing while xfce4-appearance-settings is opened.

In macOS it works properly: such apps are launched only once and when clicking again on the app icon, it moves to the foreground rather than starting a new obsolete instance.

How can this be realized in Ubuntu ?

2 Answers2

1

It is the application itself that controls that behavior.

A command, e.g. for firefox, that switches to a running window, and only launches the program if it is not running, looks like:

wmctrl -x -a firefox || firefox

In bash, the command separator || means "OR". The first command, wmctrl, attempts to switch to a window of WM_CLASS "firefox". If that fails, firefox is executed.

The first instance firefox refers to the WM_CLASS of a firefox window. You can see the WM_CLASS of running windows in the output of wmctrl -lx.

To use this for a shortcut key, or in a .desktop launcher, use following command:

sh -c "wmctrl -x -a firefox || firefox"
vanadium
  • 97,564
0

To build on vanadium's answer, you can make a generic executable that will do this for any application's .desktop file, effectively giving you the configuration at-will.

Create a file in your home folder. I called it switch-to-if-open. No extension is needed.

touch switch-to-if-open.

In your editor of choice, put the following:

#!/bin/sh
wmctrl -x -a $1 || "$@"

$1 is the application name and "$@" runs the full command with all additional arguments defined in the .desktop file, including field codes (like the url %u used for firefox).

Make it executable:

sudo chmod +x switch-to-if-open

In your .desktop file (using firefox as an example), under the [Desktop Entry] section, change:

Exec: firefox %u

to:

Exec: /home/${user}/switch-to-if-open firefox %u

Replace ${user} here with your username.

You may want to do this for any other subsequent [Desktop Action ...] sections in the .desktop file if applicable.

If you want to use this for all users of your system, the file should be placed in /usr/local/bin instead of your home directory.

If you want to avoid referencing the full path you should be able to add it to your $PATH or create a symlink. Not sure if the .desktop file cares which method you use here.

You should test this with applications that make use of complex commands with multiple parameters and/or a field code to make sure they still behave as expected. I use a GNOME desktop so I used gio to test if the url (%u field code) still passes through. I believe exo-open can be used for the same purpose in xfce.