#!/bin/zsh setopt no_unset warn_create_global extended_glob typeset -gA app_match_title app_match_class app_cmd typeset -g app_id=0 add_app() { app_match_title[$app_id]=$1 app_match_class[$app_id]=$2 shift 2 app_cmd[$app_id]="${(pj:\0:)@}" app_id=$(( $app_id + 1 )) } launch_app() { "${(0@)app_cmd[$1]}" &! } typeset -f -t launch_app add_app '*weechat*' '"weechat:*" "URxvt"' c.tmux weechat.ccx -s weechat weechat-curses add_app '*profanity*' '"profanity:*" "URxvt"' c.tmux profanity.ccx -s profanity profanity add_app '*iamb*' '"iamb:*" "URxvt"' c.tmux iamb.ccx -s iamb iamb add_app '\[telegram.ccx:*' '"telegram-desktop" "TelegramDesktop"' xsvc-in-container-xpra telegram.ccx telegram-desktop add_app '\[signal.ccx:*' '"signal" "Signal"' xsvc-in-container-xpra signal.ccx signal-desktop add_app '\[signal2.ccx:*' '"signal" "Signal"' xsvc-in-container-xpra signal2.ccx signal-desktop main() { local winid title class app xwininfo_full="$(xwininfo -tree -root)" || exit $? sed <<<"$xwininfo_full" -nEe '/^ *0x[0-9a-f]+ ".*\+[0-9]+\+[0-9]+$/{s/^ *(0x[0-9a-f]+) "(.*)": \((.*)\) [0-9]+x[0-9]+\+[0-9]+\+[0-9]+ \+[0-9]+\+[0-9]+$/\1\t\2\t\3/;p}' |\ while IFS=$'\t' read winid title class; do for app in ${(k)app_match_title}; do if [[ $title == $~app_match_title[$app] && $class == $~app_match_class[$app] ]]; then unset "app_match_title[$app]" unset "app_match_class[$app]" printf 'window %s matches application %s\n' $winid $app fi done done for app in ${(k)app_match_title}; do launch_app $app done } #set -x main