annotate python/window_title.py @ 375:9314c1008189

aslightly better; still trouble at da end
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 24 Jul 2013 03:10:27 -0700
parents 83bc1e6e5b54
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
129
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 get the active window title
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import re
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import subprocess
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 def active_window_id():
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 process = subprocess.Popen(['xprop', '-root'], stdout=subprocess.PIPE)
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 stdout, stderr = process.communicate()
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 for line in stdout.splitlines():
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 if '_NET_ACTIVE_WINDOW(WINDOW):' in line:
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 return line.rsplit(None, 1)[-1]
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 def window_title(window_id):
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 process = subprocess.Popen(['xprop', '-id', window_id], stdout=subprocess.PIPE)
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 stdout, stderr = process.communicate()
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 for line in stdout.splitlines():
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 match = re.match("WM_NAME\((?P<type>.+)\) = (?P<name>.+)", line)
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 if match:
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 type = match.group("type")
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if type == "STRING" or type == "COMPOUND_TEXT":
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 return match.group("name").strip('"')
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 def active_window_title():
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 return window_title(active_window_id())
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 def main():
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 title = active_window_title()
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 print title
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 if __name__ == '__main__':
83bc1e6e5b54 part of a dream realized
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 main()