Skip to content Skip to sidebar Skip to footer

Get Active Window Title In X

I'm trying to get the title of the active window. The application is a background task so if the user has Eclipse open the function returns 'Eclipse - blabla', so it's not getting

Solution 1:

xdotool can do that.

xdotool getactivewindow

Solution 2:

I modified your solution slightly so it should run more efficiently (it passes parameters to xprop so only the data it needs is returned). Also, I'm not sure it's necessary to buffer the output of xprop so I took that out. It should also correct return "Active window not found" if for some reason it can't find the active window.

def get_active_window_title(self):
    root = Popen(['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout=PIPE)

    for line in root.stdout:
        m = re.search('^_NET_ACTIVE_WINDOW.* ([\w]+)$', line)
        if m != None:
            id_ = m.group(1)
            id_w = Popen(['xprop', '-id', id_, 'WM_NAME'], stdout=PIPE)
            breakif id_w != None:
        for line in id_w.stdout:
            match = re.match("WM_NAME\(\w+\) = (?P<name>.+)$", line)
            ifmatch != None:
                returnmatch.group("name")

    return"Active window not found"

Solution 3:

You can get the active window title with xdotool:

$ xdotool getactivewindow getwindowname

Solution 4:

This is too late to be useful but it does work and I have programs that use wnck.

The wnck example needs a call to screen.force_update() before wnck will work. Without that wnck does not have any information about the windows on the screen. That is:

defget_active_window_title(self):
    screen = wnck.screen_get_default()
    if screen isNone:
        return"Could not get screen"
    screen.force_update()
    window = screen.get_active_window()
    if window isNone:
        return"Could not get window"
    title = window.get_name()
    return title

Solution 5:

I see that the question is a bit dusty by now, also support for Python 2 is nearing the end of its scheduled lifetime, so I thought I'd mention a more Python 3'ic version.

In fact, it's probably better than just more 3-style - in Python 3 "Popen objects are supported as context managers via the with statement: on exit, standard file descriptors are closed, and the process is waited for".

Thus the below is probably more adequate and less resource hungry for newer Pythons:

(also, without withs, you might bump into 'too many open files' problems - which I of course found out about in the hard way :) - should you query for the window title frequently enough on Ubuntu ~16 .)

withPopen(['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout=PIPE) as root:
        for line in root.stdout:
            line = str(line, encoding="UTF-8")

            m = re.search('^_NET_ACTIVE_WINDOW.* ([\w]+)$', line)
            if m isnot None:
                id_ = m.group(1)
                withPopen(['xprop', '-id', id_, 'WM_NAME'],
                           stdout=PIPE) as id_w:
                    for line in id_w.stdout:
                        line = str(line, encoding="UTF-8")
                        match = re.match("WM_NAME\(\w+\) = \"(?P<name>.+)\"$",
                                         line)
                    if match isnot None:
                        return match.group("name")
                breakreturn"Active window not found"

Post a Comment for "Get Active Window Title In X"