I have the answer for 2022, it's quite powerful one based on The newest wmctrl logic, welp it mimics autohotkey on windows
So first we need to get the active desktop / workspace
wmctrl -d | grep "*" | awk --field-separator=" " '{print $1}'
After that, you can piece it to get the list ONLY Window on That desktop
D=$(wmctrl -d | grep "*" | awk --field-separator=" " '{print $1}')
WINNAME="GVIM"
# Or you can get the full name as hex, more accurate for same window name
WIN=$(wmctrl -l | grep " $DESKTOP " | grep -i $WIN | awk '{print $1}')
# WIN will be filled with 0xYYYY ID in hex or use simple winname
IN=$(wmctrl -l | grep " $DESKTOP " | grep -i gvim | awk --field-separator="$(hostname) " '{print $2}')
# Get the number of window contain it's name
STATUS=$(wmctrl -l | grep " $DESKTOP " | grep -i $WINNAME | wc -l)
Then you can check it using if like this (For winname)
if [ $STATUS -gt 0 ]
then
activeWindow=$(xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME | cut -d '"' -f 2)
# zenity --notification --text="$activeWindow"
if [[ $activeWindow =~ "$WINNAME" ]]
then
wmctrl -r "$WIN" -b toggle,hidden
else
wmctrl -a "$WIN"
fi
else
/usr/bin/gvim # if the app hasn't started yet, start it
fi
OR
For Hex ID
# ... output omitted
if [[ $activeWindow =~ "$WINNAME" ]]
then
wmctrl -i -r "$WIN" -b toggle,hidden
else
wmctrl -i -a "$WIN"
fi
# ... output omitted, see source before
So if you piece it all together (for using window name, this is not bullet prof as if has same name between workspace, well, it will follow the other workspace)
#!/bin/bash
DESKTOP=$(wmctrl -d | grep "*" | awk --field-separator=" " '{print $1}')
STATUS=$(wmctrl -l | grep " $DESKTOP " | grep -i gvim | wc -l)
WIN=$(wmctrl -l | grep " $DESKTOP " | grep -i gvim | awk --field-separator="$(hostname) " '{print $4}')
# zenity --notification --text="$STATUS $DESKTOP $WIN"
if [ $STATUS -gt 0 ]
then
activeWindow=$(xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME | cut -d '"' -f 2)
# zenity --notification --text="$activeWindow"
if [[ $activeWindow =~ "GVIM" ]]
then
wmctrl -r $WIN -b toggle,hidden
else
wmctrl -a $WIN
fi
else
/usr/bin/gvim
fi
OR BULLET PROF using HEX ID
#!/bin/bash
DESKTOP=$(wmctrl -d | grep "*" | awk --field-separator=" " '{print $1}')
STATUS=$(wmctrl -l | grep " $DESKTOP " | grep -i gvim | wc -l)
WIN=$(wmctrl -l | grep " $DESKTOP " | grep -i gvim | awk '{print $1}')
#zenity --notification --text="$WIN"
if [ $STATUS -gt 0 ]
then
activeWindow=$(xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME | cut -d '"' -f 2)
#zenity --notification --text="$STATUS $DESKTOP \"$WIN\" $activeWindow"
if [[ $activeWindow =~ "GVIM" ]]
then
wmctrl -i -r "$WIN" -b toggle,hidden
else
wmctrl -i -a "$WIN"
fi
else
/usr/bin/gvim
fi
-i in man wmctrl
means :
-i Interpret window arguments () as a numeric value rather than a string name for the window. If the numeric value
starts with the prefix '0x' it is assumed to be a hexadecimal number
You can get window toggle like AHK Script on Windows, but more powerful than it should.
This script tested on XFCE, and works, either Xubuntu 22.04 or Fedora 36.