Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@psi-4ward
Last active March 19, 2023 21:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psi-4ward/18c3a596d28a3cfa772101af755545df to your computer and use it in GitHub Desktop.
Save psi-4ward/18c3a596d28a3cfa772101af755545df to your computer and use it in GitHub Desktop.
Linux window activation by hotkey
# Meta c brings Chrom(e|ium) to foreground
"~/.dotfiles/window-activator.sh -a Chrom"
Mod4+c
# Meta k brings Konsole to foreground
"~/.dotfiles/window-activator.sh -a Konsole"
Mod4+k
# Meta k brings WebStorm to foreground
"~/.dotfiles/window-activator.sh -a WebStorm"
Mod4+s
# Meta d brings Dolphin to foreground
"~/.dotfiles/window-activator.sh -a Dolphin"
Mod4+d
# Meta e brings Thunderbird to foreground
"~/.dotfiles/window-activator.sh -a Thunderbird"
Mod4+e

Bring a window to foreground using a hotkey

A helper script for Linux (xorg) to bring a window to foreground when hitting a hotkey.
It respects the recent used window so if you have 5 Chromium instances, it will bring up the recently used one.

How

It uses xprop -spy to listen for window focus changes and persists the window-id to a temporaray file if the window title includes a given app name.

wmctrl brings the recently used window-id for a given app name to foreground.

xbindkeys listes for the hotkey to trigger window-activator.sh

Setup

  1. download the window-activator.sh and make it executeable (chmod +x window-activator.sh)
  2. create any kind of autostart for the daemon, ie ~/.config/autostart/window-activator.desktop
    [Desktop Entry]
    Exec=~/.dotfiles/window-activator.sh -d Chrom Thunderbird Dolphin WebStorm
    StartupNotify=false
    Terminal=false
    Type=Application
    
    PS: Adjust the list of monitored apps
  3. Add some .xbindkeysrc config to trigger window-activator.sh -a <app> using a hotkey see the example below
#!/bin/bash
TMPDIR=/tmp/app-hotkeys
if ! command -v wmctrl >/dev/null 2>&1 ; then
echo >&2 "Error: wmctrl not found. You need to install wmctrl."
exit 1
fi
if ! command -v xprop >/dev/null 2>&1 ; then
echo >&2 "Error: xprop not found. You need to install xorg-xprop."
exit 1
fi
function activate() {
if [ -z "$1" ] ; then
>&2 echo "Error: app param not specified"
exit 1
fi
if [ ! -e "$TMPDIR/$1" ]; then
>&2 echo "$TMPDIR/$1 not found. Is the daemon runnung and $1 a monitored app and was focues at least one time?"
exit 1
fi
wmctrl -i -a $(cat $TMPDIR/$1) &>/dev/null
}
function daemon() {
mkdir -p $TMPDIR
while read WID ; do
WID=$(echo $WID | awk -F '# ' '{print $2}')
APP=$(xprop -notype -id $WID _NET_WM_NAME | awk -F '"' '{print $2}')
for i in "${APPS[@]}"; do
if echo $APP | grep -qF $i ; then
echo $WID > $TMPDIR/$i
fi
done
done < <(xprop -spy -root _NET_ACTIVE_WINDOW)
}
case "$1" in
"-d")
shift
APPS=( "$@" )
daemon
;;
"-a")
activate $2
;;
*)
echo "Linux app activator"
echo "Usage:"
echo " $0 -d <app1> <app2> ... Start daemon and monitor Apps"
echo " $0 -a <app> Activate <app> window"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment