Ubuntu: How to send a "close" command to a window with a title matching a regex?

5

3

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?

GJ.

Posted 2010-09-18T13:05:43.620

Reputation: 8 151

Answers

11

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.

Gilles 'SO- stop being evil'

Posted 2010-09-18T13:05:43.620

Reputation: 58 319

Thanks a lot. However, when I'm connected via ssh wmctrl returns "Cannot open display". Is there a way around that? – GJ. – 2010-09-19T06:08:27.790

@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 'SO- stop being evil' – 2010-09-19T10:49:59.203