Keyboard shortcut switch to instance

If you create a keyboard shortcut to start a program the program will start when you press the key combination. So far manjaro/xfce and windows works the same way. If you alt-tab to another window and then press the key combination again, manjaro will start a new instance of the program, while Windows brings focus to the instance already running. Is there any way to bring the windows behavior to manjaro?
Basically I’d like the key combination to test if a instance of the program is running. If it’s running set focus on that instance. If it’s not running start a new instance. While the current behavior is to always start a new instance.

This Xfce Forum thread provides a script that does what you want:
https://forum.xfce.org/viewtopic.php?pid=22457#p22457

You’ll need the package wmctrl for the script to work (sudo pacman -Syu wmctrl).

Create a file called launcher in the directory ~/.local/bin/ (create this directory if it doesn’t exist), paste the content from the provided script in it and make the file executable

chmod +x ~/.local/bin/launcher

After that modify the keyboard shortcut that launches the application in question and use the command

launcher app_name

(change app_name with the name of the executable that launches the application)

Tested with Firefox and Xfce4 Terminal and it seems to work as expected.

Hope that helps!

Thanks. I modified the script a bit - combined the script in the link with a version later in the same thread. My goal was that the script would work when the program is launched using full path.

#!/bin/bash

# there has to be at least one parameter, the name of the file to execute
if [ $# -lt 1 ]
then
  echo "Usage: `basename $0` {executable_name parameters}"
  exit 1
fi

BNAME=`basename $1`

# test to see if program is already running
if [ "`wmctrl -lx | tr -s ' ' | cut -d' ' -f1-3 | grep -i $BNAME`" ]; then
        # means it must already be running

        # now let's make sure all related windows are raised and brought to the front
        for win in `wmctrl -lx | tr -s ' ' | cut -d' ' -f1-3 | grep -i $BNAME | cut -d' ' -f1`
        do
                wmctrl -i -a $win
        done
else
        # start it up
        $*&
fi
exit 0

Put the script in /usr/local/bin
chmod +x /usr/local/bin/launch
Create keyboard shortcuts to run
/user/local/bin/launch /path/to/my/prog

By using this on the keyboard shortcuts only, the keyboard will switch to running instance, while selecting the program from menu will create a new instance.

1 Like

For this to work, you have to copy the .desktop file of the application from the /usr/share/applications/ directory to the ~/.local/share/applications/ directory and change the line beginning with Exec so that it becomes similar to this:

Exec= launcher app_executable_file

It’s my intention to have the keyboard shortcut switch to running instance, and the menu option to start a new instance.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.