Created
July 23, 2024 15:40
-
-
Save apolopena/41fab8181d95191736423e3ab56414f1 to your computer and use it in GitHub Desktop.
parse_wmctrl.sh
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
#!/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