2

I have Ubuntu 20.

I want to run only one Thunar instance for user.

I tried: wmctrl - not work, thunar window is not in result

How to do this?

Krzysztof
  • 131
  • I can think of an option with a script replacing thunar from /usr/bin, but it's not very pretty – Giorgos Saridakis Mar 06 '21 at 11:21
  • wmctrl on its own just displays the help page for me. So probably you tried something else? I use jumpapp to launch or switch to if it is already running. It is a bash script that uses wmctrl. – vanadium Mar 06 '21 at 11:50
  • @vanadium I had more of a custom script in mind, something to grep through the processes of a user and if the real thunar binary is running to exit, otherwise run it. by the way, thunar allows only one window if not run by a terminal – Giorgos Saridakis Mar 06 '21 at 13:07

1 Answers1

1

Launch or Switch To

If the application by itself does not allow to connect to a running instance when it is run a second time, you can use wmctrl to switch to a running instance if present.

Following one liner will do:

wmctrl -x -a Thunar || thunar

This runs wmctrl, which will switch (-a, activate) to a window with class (-x) "Thunar". If the command fails, i.e., there is no such window, then the command after || is run, the command to start thunar. Find the correct window class (not sure if "Thunar" is valid) in the output of wmctrl -lx.

Adapting the launcher

You now can customize the launcher for Thunar in your menu. Launchers are text files with the .desktop extension. They live (among others) in /usr/share/applications. Copy the launcher of Thunar to your ~/.local/share/applications folder:

cp /usr/share/application/thunar.desktop ~/.local/share/applications

Then edit the copy and change the Exec= line to something like:

Exec=sh -c "wmctrl -x -a Thunar || thunar %U"

You enclose the command (which in reality is two commands) in a call to bash (sh -c) because launchers do not support having more than one command on the Exec is line. thunar %U should be the command that originally appeared in the launcher.

**Adapting a hotkey to launch Thunar"

If you use a hotkey to launch Thunar, you can instead specify sh -c "wmctrl -x -a Thunar || thunar as the command. Now, pressing the hotkey will bring a running instance in front if any is there.

Optional tip

Instead of directly using wmctrl, I use the bash script jumpapp (https://github.com/mkropat/jumpapp), which does the same but on steroids. Subsequent invocations will cycle through all open windows. The script has different options to cope with the particular behaviour of different applications.

vanadium
  • 88,010