Skip to content

Instantly share code, notes, and snippets.

@deafmute1
Last active August 24, 2020 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deafmute1/d9455746f07fa0c4a304d1aa931cd934 to your computer and use it in GitHub Desktop.
Save deafmute1/d9455746f07fa0c4a304d1aa931cd934 to your computer and use it in GitHub Desktop.
#!/bin/bash
# REQUIRES: xdotool wmctrl
# AUTHOR: Ethan Djeric <me@ethandjeric.com>
# CONFIRMED WM COMPATIBILITY: kwin
# Usage:/path/to/toggle_front.sh </path/to/BINARY> <WINDOW CLASS>
# /path/to/BINARY can be any callable command from $PATH that runs the program.
# <WINDOW CLASS> can be found by taking the constant part of the name present in the window bar;
# typically the format is <INSTANCE NAME> - <WINDOW NAME>.
# e.g. ~:fish - Konsole would be a instance of Konsole running a fish shell.
# e.g. To use this script with Konsole: /path/to/toggle_front.sh konsole Konsole
# This script will not work if part of the window name is not constant across all instances of it,
# although this is irregular behaviour.
binary=$1
wind_title=$2
if [[ "$(xdotool getactivewindow getwindowname)" == *"$wind_title"* ]]; then
xdotool windowminimize $(xdotool getactivewindow)
else
windId=$(wmctrl -l | awk -v pat="$(wmctrl -d | grep "*" | cut -d ' ' -f1)" '$2 ~ pat' | grep "$wind_title" | cut -d ' ' -f1)
wmctrl -ia $windId || $binary & # using xdotool windowraise doesn't work here for kwin.
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment