Detecting currently active window

Install xdotool, then run

xdotool getwindowfocus getwindowname

It will give e.g. for the current webpage opened in Firefox :

linux - Detecting currently active window - Super User - Mozilla Firefox

This is more direct and only uses xprop and cut:

xprop -id $(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2) _NET_WM_NAME

These commands are just an extraction of properties from the root window and the application window, but per Lorenzo von Matterhorn's request:

  • First,

    xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW
    

    Extracts the _NET_ACTIVE_WINDOW property from the root, which gives you the XID of the active window. The 32x '\t$0' tells xprop to format the output in a way that cut can easily parse later.

  • Then, extract just the XID from the output with cut -f 2

  • Then, pass the XID in as a parameter to

    xprop -id XID _NET_WM_NAME
    

Which prints the name of that window.


There is, but no short answer or solution.

$ wmctrl -lp | grep $(xprop -root | grep _NET_ACTIVE_WINDOW | head -1 | \
    awk '{print $5}' | sed 's/,//' | sed 's/^0x/0x0/')

result:

0x03800004  0 16459  xxxxxxxxxx /bin/bash

In use:

$ for x in $(seq 1 10); do sleep 5; wmctrl -lp | grep $(xprop -root | \
    grep _NET_ACTIVE_WINDOW | head -1 | awk '{print $5}' | sed 's/,//' | \
    sed 's/^0x/0x0/'); done

0x03800004  0 16459  xxxxxxxxxx /bin/bash
0x020000a4  0 13524  xxxxxxxxxx linux - Detecting currently active window - Super User - Mozilla Firefox (Build 20120129142219)