Detecting currently active window

53

19

The linux commandline tool wmctrl allows you to list all windows, or all desktops, among other things. In the desktop-listing mode, the current desktop is marked with an asterisk.

I need a tool that can figure out the currently active window's title. Unfortunately, wmctrl doesn't have a helper that does this - despite it knowing which window is currently active (see :ACTIVE: marker).

Is there another commandline tool that can give me the window id and/or window title of the current window?

Dave Vogt

Posted 2012-01-26T12:39:55.693

Reputation: 1 470

Answers

56

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

Skippy le Grand Gourou

Posted 2012-01-26T12:39:55.693

Reputation: 1 349

Powerful tool! Apart from getting current active window, it also has lots of other useful functionalities. – Searene – 2014-10-10T01:25:00.860

Compared to Jim Paris's answer, this has the disadvantage of a dependency to install, but it seems to run faster, particularly if xdotool is already in the disk cache. – mc0e – 2015-01-08T07:52:51.963

1If you want to manipulate the current window with wmctrl, you could do: wmctrl -i -r \xdotool getwindowfocus` -e 0,10,10,-1,-1, where-i` tells wmctrl to expect the window id rather than the window title. – fiedl – 2015-01-21T01:29:10.940

34

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.

Jim Paris

Posted 2012-01-26T12:39:55.693

Reputation: 441

1hi there, can you please explain the arguments and a bit of the syntax? it may be helpful for the QA and future readers. – Lorenzo Von Matterhorn – 2013-01-12T21:34:03.573

9

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)

tao

Posted 2012-01-26T12:39:55.693

Reputation: 1 355

1This looks like a good solution, didn't know about the xprop utility. In the meantime, I've patched wmctrl to provide such an option and sent it to the author. Unfortunately, I didn't get a response yet. – Dave Vogt – 2012-02-22T09:15:02.217

1+1 for effectiveness and "simplicity" on the code – Lorenzo Von Matterhorn – 2013-01-12T21:51:21.897

5

This is what worked for me:

xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME

Gives:

WM_NAME(STRING) = "~"

So maybe you'd do a little sed ugliness to get the name all by itself:

xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME | sed -e 's/.*"\(.*\)".*/\1/'

Gives:

~

Which is my currently focussed window name, all by itself.

Or, avoid sed with the slightly less ugly:

xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME | awk -F '"' '{print $2}'

Greg Bell

Posted 2012-01-26T12:39:55.693

Reputation: 373

1Or since cut has laready been introduced, you could use that again: xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME | cut -d '"' -f 2 – mc0e – 2015-01-08T07:50:17.697

3

Relatively short, and handles window names containing quotes correctly:

xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME | 
sed -nr 's/.*= "(.*)"$/\1/p'

Ton van den Heuvel

Posted 2012-01-26T12:39:55.693

Reputation: 3 316