i3-get-window-criteria 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/sh
  2. ## From: https://gist.github.com/jottr/8645010
  3. # i3-get-window-criteria - Get criteria for use with i3 config commands
  4. # To use, run this script, then click on a window.
  5. # Output is in the format: [<name>=<value> <name>=<value> ...]
  6. # Known problem: when WM_NAME is used as fallback for the 'title="<string>"' criterion,
  7. # quotes in "<string>" are not escaped properly. This is a problem with the output of `xprop`,
  8. # reported upstream: https://bugs.freedesktop.org/show_bug.cgi?id=66807
  9. PROGNAME=`basename "$0"`
  10. # Check for xwininfo and xprop
  11. for cmd in xwininfo xprop; do
  12. if ! which $cmd > /dev/null 2>&1; then
  13. echo "$PROGNAME: $cmd: command not found" >&2
  14. exit 1
  15. fi
  16. done
  17. match_int='[0-9][0-9]*'
  18. match_string='".*"'
  19. match_qstring='"[^"\\]*(\\.[^"\\]*)*"' # NOTE: Adds 1 backreference
  20. {
  21. # Run xwininfo, get window id
  22. window_id=`xwininfo -int | sed -nre "s/^xwininfo: Window id: ($match_int) .*$/\1/p"`
  23. echo "id=$window_id"
  24. # Run xprop, transform its output into i3 criteria. Handle fallback to
  25. # WM_NAME when _NET_WM_NAME isn't set
  26. xprop -id $window_id |
  27. sed -nr \
  28. -e "s/^WM_CLASS\(STRING\) = ($match_qstring), ($match_qstring)$/instance=\1\nclass=\3/p" \
  29. -e "s/^WM_WINDOW_ROLE\(STRING\) = ($match_qstring)$/window_role=\1/p" \
  30. -e "/^WM_NAME\(STRING\) = ($match_string)$/{s//title=\1/; h}" \
  31. -e "/^_NET_WM_NAME\(UTF8_STRING\) = ($match_qstring)$/{s//title=\1/; h}" \
  32. -e '${g; p}'
  33. } | sort | tr "\n" " " | sed -r 's/^(.*) $/[\1]\n/'