tags:

views:

71

answers:

2

I'm running gnome and have a program that spawns off a large 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 ?

+2  A: 

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.)

jleedev
I tested this and it works, +1
DoR
+3  A: 

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)
sgargan