trying to get WM_CLASS of active window to make AutoKey script for hotkey that works differently for different applications

here is one way to do it:

winname=`xdotool getactivewindow getwindowname`
winclass=`xprop -name "$winname" | grep WM_CLASS`

it works as desired when active window's title contains only ANSI characters. if there are other characters (like letters of non-latin alphabets) then xprop exits with error.

is there a way around this shortcoming of xprop?

or what are other ways to get WM_CLASS or otherwise identify active window's program name?


You are close. Try this:

winid=$(xdotool getactivewindow)
winclass=$(xprop -id "$winid" | grep WM_CLASS)

I tested it with Russian alphabet and it worked, but the class name will be displayed in octal numbers (e.g., for "алфавит" I got "\320\260\320\273\321\204\320\260\320\262\320\270\321\202").

Notice I replaced the backticks, which are not recommended, by $(...).

  • thanks, it works indeed. I guess octal encoding should not be a problem for equality test in "if" condition. why backticks are not recommended? – Mikhail Feb 2 '20 at 12:07
  • Mainly for clarity reasons when nesting. See this: mywiki.wooledge.org/BashFAQ/082 – user596332 Feb 2 '20 at 14:29
  • For your equality test, see the difference between: if [ "$(printf "\321\204")" = "ф" ]; then echo Equal; fi vs if [ "\321\204" = "ф" ]; then echo Equal; fi – user596332 Feb 2 '20 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.