Skip to content

Instantly share code, notes, and snippets.

@apolopena
Created July 23, 2024 15:40
Show Gist options
  • Save apolopena/41fab8181d95191736423e3ab56414f1 to your computer and use it in GitHub Desktop.
Save apolopena/41fab8181d95191736423e3ab56414f1 to your computer and use it in GitHub Desktop.
parse_wmctrl.sh
#!/usr/bin/env bash
####################
parse_wmctrl.sh
Author: apolopena
Desc: Get a window name by index from wmctrl
Usage: ./parse_wmctrl.sh get 3
####################
# Globals
window_list=()
get() {
local limit ep="Error:"
if [[ ! "$1" =~ ^[0-9]+$ ]]; then
echo "${ep} argument '${1}' must be a number"; exit 1
fi
[[ ${#window_list[@]} -eq 0 ]] && _init
limit=$((${#window_list[@]} - 1))
if [[ $1 -eq 0 || $1 -gt $limit ]]; then
echo "${ep}: illegal range value: ${1}"
echo "Legal range values are: 1 - ${limit}"
fi
echo "${window_list[$1]}" | cut -d ' ' -f 4
}
_init() {
while IFS= read -r line; do
window_list+=("$line")
done <<< "$(wmctrl -lx)"
}
# Call functions gracefully
if declare -f "$1" > /dev/null; then "$@"; else echo "'$1' is not a known function name" >&2; exit 1; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment