8

After stackoverflow answered my previous question on here about my Wiimote left/right click issue, Not only can I move the mouse cursor, I can now left/right click on things. I now have one more question.

What do I use in python to get the title of the current active window? After googling 'X11 Python Window Title', 'Linux Python Window Title' and things similar, All I've found is win32 and tkinker (again?), which isn't what I need.

If you could help, That would be awesome!

jww
  • 97,681
  • 90
  • 411
  • 885
dbdii407
  • 815
  • 3
  • 11
  • 14

7 Answers7

12

EDIT

best way:

import gtk
import wnck
import glib

class WindowTitle(object):
    def __init__(self):
        self.title = None
        glib.timeout_add(100, self.get_title)

    def get_title(self):
        try:
            title = wnck.screen_get_default().get_active_window().get_name()
            if self.title != title:
                self.title  = title
                print title
        except AttributeError:
            pass
        return True

WindowTitle()
gtk.main()

Alternative way:

from subprocess import PIPE, Popen
import time

title = ''
root_check = ''

while True:
    time.sleep(0.6)
    root = Popen(['xprop', '-root'],  stdout=PIPE)

    if root.stdout != root_check:
        root_check = root.stdout

        for i in root.stdout:
            if '_NET_ACTIVE_WINDOW(WINDOW):' in i:
                id_ = i.split()[4]
                id_w = Popen(['xprop', '-id', id_], stdout=PIPE)

        for j in id_w.stdout:
            if 'WM_ICON_NAME(STRING)' in j:
                if title != j.split()[2]:
                    title = j.split()[2]
                    print "current window title: %s" % title
killown
  • 4,497
  • 3
  • 25
  • 29
  • this only gives me the title of the window that it's running on. :( – dbdii407 Aug 24 '10 at 00:12
  • just put a time.sleep and change for other window and you will get the title of the current window. this get the title of current window, and not only the window where it's running – killown Aug 24 '10 at 00:42
  • Actually, I did set a time.sleep(3), It still was the same title – dbdii407 Aug 24 '10 at 00:50
6

I noticed that wnck requires GTK event loop to update the active window. There is no such problem with Xlib:

import Xlib
import Xlib.display
disp = Xlib.display.Display()
window = disp.get_input_focus().focus

# Get active window class and name
window.get_wm_class()
window.get_wm_name()
Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92
3

killown's xprop-based solution can be compacted into a single (though lengthy) statement:

import subprocess
def GetActiveWindowTitle():
    return subprocess.Popen(["xprop", "-id", subprocess.Popen(["xprop", "-root", "_NET_ACTIVE_WINDOW"], stdout=subprocess.PIPE).communicate()[0].strip().split()[-1], "WM_NAME"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip().split('"', 1)[-1][:-1]
KeyJ
  • 39
  • 1
2

With ewmh:

from ewmh import EWMH
wm = EWMH()
win = wm.getActiveWindow()
win_name = win.get_wm_name()
print(win_name)
Eyal Levin
  • 16,271
  • 6
  • 66
  • 56
0

My solution:

import wnck
disp=Display()
default_screen=wnck.screen_get_default()
default_screen.force_update()

active_window=disp.create_resource_object('window', default_screen.get_active_window().get_xid())
title=active_window.get_wm_name()
diyism
  • 12,477
  • 5
  • 46
  • 46
0

I think python-wnck might be useful if you want to handle Windows & workspaces & such. I can't find the Python docs immediately, but according to the docs for the libwnck C library that it wraps, it has a wnck_screen_get_active_window() method.

JanC
  • 1,264
  • 9
  • 6
-1

Is the problem to find out which window is active or what the title is? Getting a window's title is easy:

MainWindow.title()

,where MainWindow is the window's name. No idea about active window though. Never had multiple windows.

Rawing
  • 19
  • 1
  • 1
  • 2