#!/usr/bin/env bash function prop () { # Needs to be declared before it’s assigned or I cannot grab the exit code # later. Fun race condition <.< local selection local window # Find all visible windows and get their geometry, then # format slurp-friendly, then # run slurp! selection=$(swaymsg -t get_tree | jq '.. | select(.visible?==true) | .rect | [ .x,.y,.width,.height ]' \ | jq -r '"\(.[0]),\(.[1]) \(.[2])x\(.[3])"' \ | slurp -r) local ret=$? [ $ret -ne 0 ] && exit $ret local x=$(echo "$selection" | awk -F'[, x]' '{print $1}') local y=$(echo "$selection" | awk -F'[, x]' '{print $2}') local w=$(echo "$selection" | awk -F'[, x]' '{print $3}') local h=$(echo "$selection" | awk -F'[, x]' '{print $4}') # Find the window matching the selection. window=$(swaymsg -t get_tree | \ jq ".. | select((.rect?.x==$x) and (.rect?.y==$y) and (.rect?.width==$w) and (.rect?.height=$h) and (.visible?==true))") [ -z "$window" ] && exit 1 # Pretty print, since capturing the output of `swaymsg` makes it uglify. echo $window | jq } function check_dependencies () { local ret=0 for cmd in "$@"; do ! [ $(command -v $cmd) ] && echo "Missing dependency: $cmd." && ret=1 done return $ret } ( check_dependencies "jq" "slurp" "swaymsg" ) || exit 1 prop