comparison python/window_title.py @ 129:83bc1e6e5b54

part of a dream realized
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 18 Mar 2011 11:33:56 -0700
parents
children
comparison
equal deleted inserted replaced
128:57069bd1a46a 129:83bc1e6e5b54
1 #!/usr/bin/env python
2
3 """
4 get the active window title
5 """
6
7 import re
8 import subprocess
9
10 def active_window_id():
11 process = subprocess.Popen(['xprop', '-root'], stdout=subprocess.PIPE)
12 stdout, stderr = process.communicate()
13 for line in stdout.splitlines():
14 if '_NET_ACTIVE_WINDOW(WINDOW):' in line:
15 return line.rsplit(None, 1)[-1]
16
17 def window_title(window_id):
18 process = subprocess.Popen(['xprop', '-id', window_id], stdout=subprocess.PIPE)
19 stdout, stderr = process.communicate()
20 for line in stdout.splitlines():
21 match = re.match("WM_NAME\((?P<type>.+)\) = (?P<name>.+)", line)
22 if match:
23 type = match.group("type")
24 if type == "STRING" or type == "COMPOUND_TEXT":
25 return match.group("name").strip('"')
26
27 def active_window_title():
28 return window_title(active_window_id())
29
30 def main():
31 title = active_window_title()
32 print title
33
34 if __name__ == '__main__':
35 main()