Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to programatically close a window in some program, based on the window title.

Is there a tool or a built-in system command I can use for that?

share|improve this question
up vote 8 down vote accepted

wmctrl can close a window based on a substring or exact match of the window title (wmctrl -c 'substring' or wmctrl -F -c 'exact match').

If you need more control, you can parse wmctrl -l, extract the matching window IDs and pass them to wmctrl -i -c. You can use wmctrl -x -l and match the class as well; in fact, I recommend it, since applications you haven't thought about might have windows with the same title.

share|improve this answer
Thanks a lot. However, when I'm connected via ssh wmctrl returns "Cannot open display". Is there a way around that? – GJ. Sep 19 '10 at 6:08
@GJ: Programs normally connect to the display indicated by the DISPLAY environment variable. If you're connected via ssh and have X forwarding enabled, wmctrl acts on your display; without X forwarding DISPLAY won't be set. You'll also have to provide the cookie: that's a password that's generated when you log in and stored in the file indicated in the XAUTHORITY environment variable. Read this, and if you still can't figure it out, ask another question on acting remotely over an X display. – Gilles Sep 19 '10 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .