how to set a hotkey to switch to open window of a specific application if no window is present launch the application

I want to have specific hotkey for launching applications, but if already launched, it should pop up the window of the application.

so for example I press super + f then it should show me the open firefox window, if there is no open firefox window, it will launch a new one.

I only have seen that I can use super + numbers for accessing from the dock, which is quite similiar.

does anyone now if something like this is possible?


Solution 1:

Yes, it's possible. You can use pgrep to find if the application is currently running, and xdotool windowactivate to put it on the foreground, or just start it if not running.

For example, I use XFCE Mousepad as the text editor:

#!/bin/bash
if ! pgrep mousepad > /dev/null ; then
    # app isn't running, so start it
    mousepad &
    exit 0
fi
# mousepad is running, so let's get his pid
MOUSEPAD_PID=`pidof mousepad | tail -1` #if there are multiple, get the last
MOUSEPAD_WINDOW=`xdotool search --pid $MOUSEPAD_PID | tail -1` # last window
xdotool windowactivate $MOUSEPAD_WINDOW # activate the window

You can use a variable for the app, and validate if the app is valid. In any case, it's your gun, your foot.