Is there a way to simulate a "Close" event on various windows using the terminal?
Solution 1
I believe the related man page is, XKillClient. You can use xdotool
to simulate the close button being clicked from a terminal like so.
Example
Assuming I have a gnome-terminal
open and it's name is "saml@grinchy:/home".
-
Get the window ID
$ xdotool search --name "saml@grinchy:/home" 96488188
-
Send it a Alt+F4
$ xdotool windowactivate --sync 96488188 key --clearmodifiers \ --delay 100 alt+F4
You can put them together by embedding the first command into the second:
$ xdotool windowactivate --sync $( ...1st command...) key --clearmodifiers \
--delay 100 alt+F4
You can save yourself by letting xdotool
do both at the same time:
$ xdotool search --name "saml@grinchy:~" key alt+f4
Globally
You can adapt what I've provided to run it on windows that have the same name:
$ xdotool search --name "saml@grinchy:~"
96488779
96468996
Or on windows by other attributes. You can use xwininfo
to find out more about a particular window. Run it and then just click on the window of interest:
$ xwininfo
xwininfo: Please select the window about which you
would like information by clicking the
mouse in that window.
xwininfo: Window id: 0x5c04d4b "saml@grinchy:~"
Absolute upper-left X: 14
Absolute upper-left Y: 74
Relative upper-left X: 14
Relative upper-left Y: 74
Width: 941
Height: 361
Depth: 32
Visual: 0x62
Visual Class: TrueColor
Border width: 0
Class: InputOutput
Colormap: 0x5c00003 (not installed)
Bit Gravity State: NorthWestGravity
Window Gravity State: NorthWestGravity
Backing Store State: NotUseful
Save Under State: no
Map State: IsViewable
Override Redirect State: no
Corners: +14+74 -485+74 -485-465 +14-465
-geometry 132x24+14+74
Other useful tools when dealing with X11 windows are xdpyinfo
& xprop
. xdpyinfo
can be used to find out information about the X server. So you can figure out which window has focus:
$ xdpyinfo |grep focus
focus: window 0x5c00005, revert to Parent
xprop
and xwininfo
can take a -id
switch so you can provide them the Window ID that you're interested in instead of having to click on it:
$ xprop -id 0x5c00001|grep -i class
WM_CLASS(STRING) = "gnome-terminal", "Gnome-terminal"
References
Solution 2
I found xdotool
to be flaky/buggy, sometimes it closes the foreground window instead of the one that should be closed. This seems to be due to the way that keys are sent after bringing the window to the foreground instead of directly sending window events, and it's a very annoying issue. I suggest using wmctrl
, which directly closes a window without sending keystrokes.
You can close a window directly by matching on the name, e.g. both these will close the "Untitled Document 1 - gedit" window:
wmctrl -c "gedit"
wmctrl -c "Untitled"
You can use the -F
option to only consider exact matches:
wmctrl -F -c "Untitled Document 1 - gedit"
Or you can give the id directly:
wmctrl -i -c "121634821"
More usage examples/documentation can be found here.
One thing I do find very useful is xdotool
's ability to wait until there is a result using the --sync
argument. Combining the two in one command is done like this:
xdotool search --sync --name "gedit" | xargs wmctrl -i -c
If you are on a Mac, you'll probably need the -I{}
parameter:
xdotool search --sync --name "gedit" | xargs -I{} wmctrl -i -c {}
If you want to support multiple windows, you should tell xargs
to call wmctrl
with at at most 1 argument each time with the -n
option. wmctrl
does not explicitly support multiple windows as arguments:
xdotool search --sync --name "gedit" | xargs -I{} -n 1 wmctrl -i -c {}
This will wait until there is at least 1 such window, and then closes all of them.
Related videos on Youtube
Ludwig Schulze
Nothing to say about me... but if you insist, just take a look at my actions, they speak better than I could ever do. Even when I'm not part of the solution, I will not be part of the problem. Better to have nothing, no matter when, than to accept rubbish. — Random Wuxia <!> when you wish for peace and quiet, trouble tends to find ways to catch up to you. [1]: http://stackexchange.com/users/flair/416592.png "profile for Braiam on Stack Exchange, a network of free, community-driven Q&A sites" width="208" height="58" [2]: http://stackexchange.com/users/416592
Updated on September 18, 2022Comments
-
Ludwig Schulze over 1 year
I answered on Ask Ubuntu Quit all instances of
gnome-terminal
via a command but as you all can readgnome-terminal
didn't seems to have aSIG
call I could use to simulate this "Close" event. So this lead me to ask, is there a way in GNOME/KDE/LXDE/{put your window/desktop manager/environment here} to simulate the "Click in close button" event? I have read different questions that could have any relation to this, but don't answer this.What I'm looking is for a global command (if exist) to do this in different scenarios. If none exist, please explain how the "Close" button works.
Posible uses:
- I open a set of applications for a determinated task, and want to close all of them (when no longer used) in a single stroke/command without suppress any You want to save your work? alerts.
- Close all the
gnome-terminal
instances but warn me if there is child process still running. - Nothing else comes into my mind right now...
-
Ludwig Schulze almost 11 yearsNo, it won't simulate the "Close" event on any of my test (open a gedit, LibreOffice and the terminal, write something, send the command), if I use the
--signal
switch is not different ofkill
orkillall
. I'm expecting the You want to save? alert. -
schaiba almost 11 yearsYou're right, seems I misread your question.
-
Ludwig Schulze almost 11 yearsI think we can simplify even more using
xdotool search --name "saml@grinchy:~" key alt+f4
, problem is that I haven't found how to do this with several windows that have the same name... -
slm almost 11 years@Braiam - is there anything unique about them? When you invoke the window you can tell GNOME what you want the titles to be.
-
slm almost 11 years@Braiam - for example:
wmctrl -r :ACTIVE: -N "MyWindowTitle
-
slm almost 11 years@Braiam - as to putting the commands together, I'm giving you eggs, you're making the omelet 8-). Added your method to the answer too!
-
Ludwig Schulze almost 11 yearsMission accomplished! I used
xdotool search --name gedit key --window %@ alt+F4
to close all gedit instances successfully, now looking for a way to close windows that have nothing in common. -
slm almost 11 years@Braiam - I think the thing you're looking for is called a mouse 8-).
-
Ludwig Schulze almost 11 yearsMouse is overrated :/. And I solved the question,
xdotool search --name "gedit|terminal|office" key --window %@ alt+F4
will look for any window that has gedit, terminal or office in it's title name and send the alt+F4 command. -
slm almost 11 years@Braiam - nice.
-
Ludwig Schulze over 8 yearsWill I get the "are you sure" prompt dialog box?
-
jmiserez about 8 years@Braiam Yes you will, I just tested it. Only if a program is running in the terminal (e.g.
top
) of course, same as when clicking the close button. -
jarno almost 7 yearsI ran
xdotool search --sync --name "Software Updater" | xargs wmctrl -i -c
on Trusty, and started Software Updater. From now on, Software Updater does not display properly. Just the title bar displays. Reinstalling update-manager did not help. -
jarno almost 7 yearsGladly logging out and back in to Xfce session did help.
-
Gonki about 6 yearsI can't find "gedit" with either xdotool or xprop utilities. It's only true for "gedit", I can find other programs. The command xdotool search --sync --name "gedit" doesn't return result. Can anyone help? Ubuntu 17