How can I capture all windows with a specific word in their names?

I run gnome and have a program that generates a large number of separate processes, each with its own gui window. I would like to be able to selectively capture open windows whose names match a specific template to close them. Does anyone know a way to make this easy?

+2
source share
2 answers

You definitely want to use python-wnck(for documentation, you may need to search python-gnome-extrasor bind Perl or just plain C documentation). WNCK is written to make viewing screens, workspaces and windows easier. Something like that:

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 have not tested it.

( , GNOME, .)

+4

jleedev, , .

#!/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)
+4

All Articles