diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/window_title.py	Fri Mar 18 11:33:56 2011 -0700
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+"""
+get the active window title
+"""
+
+import re
+import subprocess
+
+def active_window_id():
+    process = subprocess.Popen(['xprop', '-root'], stdout=subprocess.PIPE)
+    stdout, stderr = process.communicate()
+    for line in stdout.splitlines():
+        if '_NET_ACTIVE_WINDOW(WINDOW):' in line:
+            return line.rsplit(None, 1)[-1]
+
+def window_title(window_id):
+    process = subprocess.Popen(['xprop', '-id', window_id], stdout=subprocess.PIPE)
+    stdout, stderr = process.communicate()
+    for line in stdout.splitlines():
+        match = re.match("WM_NAME\((?P<type>.+)\) = (?P<name>.+)", line)
+        if match:
+            type = match.group("type")
+            if type == "STRING" or type == "COMPOUND_TEXT":
+                return match.group("name").strip('"')
+
+def active_window_title():
+    return window_title(active_window_id())
+
+def main():
+    title = active_window_title()
+    print title
+
+if __name__ == '__main__':
+    main()