Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I answered on Ask Ubuntu Quit all instances of gnome-terminal via a command but as you all can read gnome-terminal didn't seems to have a SIGcall 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:

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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".

  1. Get the window ID

    $ xdotool search --name "saml@grinchy:/home"
    96488188
    
  2. 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

share|improve this answer
    
I 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... –  Braiam Aug 1 '13 at 16:26
    
@Braiam - is there anything unique about them? When you invoke the window you can tell GNOME what you want the titles to be. –  slm Aug 1 '13 at 16:31
    
@Braiam - for example: wmctrl -r :ACTIVE: -N "MyWindowTitle –  slm Aug 1 '13 at 16:34
    
@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! –  slm Aug 1 '13 at 16:36
    
I'm trying to use the example provided here to send the key alt+F4 to all the windows (if resize can be done, why not alt+F4), but without luck. And thanks :D. –  Braiam Aug 1 '13 at 16:46

pkill(1) should be what you need.

share|improve this answer
2  
No, 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 of kill or killall. I'm expecting the You want to save? alert. –  Braiam Aug 1 '13 at 15:32
    
You're right, seems I misread your question. –  schaiba Aug 1 '13 at 15:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.