2

我正在运行 gnome 并且有一个程序可以生成大量单独的进程,每个进程都有自己的 gui 窗口。我希望能够有选择地抓取标题与特定模式匹配的打开窗口以关闭它们。任何人都知道轻松做到这一点的方法吗?

4

2 回答 2

4

您肯定想使用python-wnck(对于文档,您可能需要查找python-gnome-extras,或 Perl 绑定,或者只是普通的 C 文档)。编写 WNCK 是为了方便查看屏幕、工作区和窗口。像这样的东西:

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)

...但我还没有测试过。

(另外,这不是 GNOME 特有的。它适用于任何桌面环境。)

于 2009-11-13T19:22:55.840 回答
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)
于 2009-11-13T21:20:33.010 回答