devdsavage

Window Criteria Script to get class

Sep 24th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # i3-get-window-criteria - Get criteria for use with i3 config commands
  4.  
  5. # To use, run this script, then click on a window.
  6. # Output is in the format: [<name>=<value> <name>=<value> ...]
  7.  
  8. # Known problem: when WM_NAME is used as fallback for the 'title="<string>"' criterion,
  9. # quotes in "<string>" are not escaped properly. This is a problem with the output of `xprop`,
  10. # reported upstream: https://bugs.freedesktop.org/show_bug.cgi?id=66807
  11.  
  12. PROGNAME=`basename "$0"`
  13.  
  14. # Check for xwininfo and xprop
  15. for cmd in xwininfo xprop; do
  16. if ! which $cmd > /dev/null 2>&1; then
  17. echo "$PROGNAME: $cmd: command not found" >&2
  18. exit 1
  19. fi
  20. done
  21.  
  22. match_int='[0-9][0-9]*'
  23. match_string='".*"'
  24. match_qstring='"[^"\\]*(\\.[^"\\]*)*"' # NOTE: Adds 1 backreference
  25.  
  26. {
  27. # Run xwininfo, get window id
  28. window_id=`xwininfo -int | sed -nre "s/^xwininfo: Window id: ($match_int) .*$/\1/p"`
  29. echo "id=$window_id"
  30.  
  31. # Run xprop, transform its output into i3 criteria. Handle fallback to
  32. # WM_NAME when _NET_WM_NAME isn't set
  33. xprop -id $window_id |
  34. sed -nr \
  35. -e "s/^WM_CLASS\(STRING\) = ($match_qstring), ($match_qstring)$/instance=\1\nclass=\3/p" \
  36. -e "s/^WM_WINDOW_ROLE\(STRING\) = ($match_qstring)$/window_role=\1/p" \
  37. -e "/^WM_NAME\(STRING\) = ($match_string)$/{s//title=\1/; h}" \
  38. -e "/^_NET_WM_NAME\(UTF8_STRING\) = ($match_qstring)$/{s//title=\1/; h}" \
  39. -e '${g; p}'
  40. } | sort | tr "\n" " " | sed -r 's/^(.*) $/[\1]\n/'
  41.  
  42. ##END OF FILE##
  43. ##Don't forget to chmod + x "filename" to make it executable###
Advertisement
Add Comment
Please, Sign In to add comment