Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
close all windows and optionally wait until some of them close
#!/bin/bash
# close all windows and optionally wait until some of them close
# Copyright (C) 2012 Vyacheslav Levit
# License: GPLv3 or later. See <http://www.gnu.org/licenses/>
function usage
{
echo "Usage: ${0##*/} [--wait [window_classes]]"
}
function some_window_open
{
count=0
while read -r line ; do
count=$((count+1))
for c in "${classes[@]}" ; do
lc="$(tr [:upper:] [:lower:] <<< "$c")"
class="$(awk '{ split($3, class, "."); \
print tolower(class[2])}' <<< "$line")"
[[ "$lc" == "$class" ]] && return 0
done
done < <(wmctrl -xl)
(( count != 0 && ${#classes[@]} == 0 )) && return 0 || return 1
}
function kill_all_windows
{
wmctrl -l | while read -r line
do
wmctrl -ic $(cut -d ' ' -f 1 <<< "$line")
done
}
function process_args
{
wait=0
if (( $# > 0 )) ; then
case $1 in
-h|--help)
usage ; exit 0 ;;
--wait)
wait=1 ; shift ; classes=("$@") ;;
*)
usage ; exit 2;
esac
fi
}
function main
{
process_args "$@"
kill_all_windows
if [[ "$wait" == 1 ]] ; then
while some_window_open ; do
sleep 1
done
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.