0

I use my function keys for specific applications (F1 chrome, F2 terminal, etc). I have a script bound to a shortcut key to activate the right application, and that works, but it doesn't activate the most-recently-focused window. The script lists windows with wmctrl -lx which seems to list windows by reverse-creation order. Is there any way to ask for or keep track of the order specific windows are activated so I can script "activate the most recent terminal window"?

Alternately, it seems if I was able to get the window stack order maintained by xfwm4 (which it uses when you cycle through windows), that seems like it would solve my problem as well but I don't see a command line interface to get at that window stack order.

1

In a script, you could use wmctrl, xprop and awk to use the activation time that is maintained by the window manager. (See e.g. wm-spec 1.5). E.g., use a (multi-line) command line like the following:

W=$(wmctrl -l | \
    awk '$ ~ "Terminal" {print $1;}' | while read x ; do \
      echo "$x $(xprop -id $(xprop -id $x \
                             | awk '$1 ~ /_NET_WM_USER_TIME/ {print $NF}' \
                             ) \
                 | awk '{print $NF}' \
                 )" ; \
      done | awk '$2>t {i=$1; t=$2} END {print i}' \
    )

In words:

  1. pass the wmctrl list through a filtering to select the window ids of the named application ("Terminal" in the example above); then
  2. pass this through (innermost) xprop to get a (pretend) window id for the last usage time, and
  3. using another (outer) xprop to get that timestamp , which is the (single) property of the "pretend window" of the _NET_WM_USER_TIME_WINDOW of the original window; then
  4. pass all the resulting id and timestamp lines through an awk filter that reports the id with the greatest timestamp.
  • This almost works but it seems some of my windows report _NET_WM_USER_TIME: not found.. Any idea why some windows have this but others don't? – Peter Lyons Apr 21 '18 at 2:44
  • No idea, really. Maybe "compositing"? Particular application(s) that evade(s) (some) window management? – Ralph Rönnquist Apr 21 '18 at 2:57
  • Maybe some windows hold the timestamp directly, without having the _NET_WM_USER_TIME_WINDOW property? The step 2-3 filtering doesn't handle that well. – Ralph Rönnquist Apr 21 '18 at 3:00

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.