开发者

How might I grab all windows with a certain word in their titles?

I'm running gnome and have a program that spawns off a lar开发者_高级运维ge number of separate processes each with its own gui window. I'd like to be able to selectively grab open windows whose titles match a certain pattern to close them. Anyone know a way to do this easily ?


You definitely want to use python-wnck (for documentation, you might need to look for python-gnome-extras, or the Perl bindings, or just the plain C documentation). WNCK is written to make it easy to look at screens, workspaces, and windows. Something like this:

import pygtk
pygtk.require('2.0')
import gtk
import wnck

screen = wnck.screen_get_default()
while gtk.events_pending():
    gtk.main_iteration()

windows = screen.get_windows()
for w in windows:
    if w.get_name() == 'foo':
        w.close(0)

...but I haven't tested it.

(Also, this won't be GNOME-specific. It works with any desktop environment.)


Great stuff jleedev, here's a minor tweak to scriptify it it and use a pattern to match the windows.

#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk
import wnck
import re
import sys

if(len(sys.argv) < 2):
  print 'A regex pattern is required to match window titles'
  print 'Usage: wkill <regex>'
  sys.exit(1)

screen = wnck.screen_get_default()
while gtk.events_pending():
    gtk.main_iteration()

titlePattern = re.compile(sys.argv[1])

windows = screen.get_windows()
for w in windows:
  if titlePattern.match(w.get_name()):
    print "Closing window - ", w.get_name()
    w.close(0)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜