Created
June 5, 2012 19:07
-
-
Save vlevit/2877044 to your computer and use it in GitHub Desktop.
close all windows and optionally wait until some of them close
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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