有没有办法使用终端在各种窗口上模拟“关闭”事件?

Bra*_*iam 17 command-line scripting x11 window window-management

我在 Ask Ubuntu Quit all instances of gnome-terminalvia a command上回答,但正如你们所有人都可以阅读的gnome-terminal那样,似乎没有SIG我可以用来模拟这个“关闭”事件的电话。所以这让我想问,在 GNOME/KDE/LXDE/{put your window/desktop manager/environment here} 中有没有办法模拟“点击关闭按钮”事件?我已经阅读 可能与此有关的不同 问题 ,但不要回答这个问题。

我正在寻找的是一个全局命令(如果存在)在不同场景中执行此操作。如果不存在,请解释“关闭”按钮的工作原理。

可能的用途:

slm*_*slm 19

我相信相关的手册页是XKillClient。您可以xdotool像这样模拟从终端单击的关闭按钮。

例子

假设我有一个gnome-terminal开放的,它的名字是“saml@grinchy:/home”。

  1. 获取窗口ID

    $ xdotool search --name "saml@grinchy:/home"
    96488188
    
    Run Code Online (Sandbox Code Playgroud)
  2. 发送Alt+F4

    $ xdotool windowactivate --sync 96488188 key --clearmodifiers \
         --delay 100 alt+F4
    
    Run Code Online (Sandbox Code Playgroud)

您可以通过将第一个命令嵌入到第二个命令中来将它们组合在一起:

$ xdotool windowactivate --sync $( ...1st command...) key --clearmodifiers \
         --delay 100 alt+F4
Run Code Online (Sandbox Code Playgroud)

您可以通过让xdotool两者同时进行来拯救自己:

$ xdotool search --name "saml@grinchy:~" key alt+f4
Run Code Online (Sandbox Code Playgroud)

全球范围内

您可以调整我提供的内容以在具有相同名称的 Windows 上运行它:

$ xdotool search --name "saml@grinchy:~"
96488779
96468996
Run Code Online (Sandbox Code Playgroud)

或者在 windows 上通过其他属性。您可以使用xwininfo来了解有关特定窗口的更多信息。运行它,然后点击感兴趣的窗口:

$ 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
Run Code Online (Sandbox Code Playgroud)

处理 X11 窗口时其他有用的工具是xdpyinfo& xpropxdpyinfo可用于查找有关 X 服务器的信息。所以你可以找出哪个窗口有焦点:

$ xdpyinfo |grep focus
focus:  window 0x5c00005, revert to Parent
Run Code Online (Sandbox Code Playgroud)

xprop并且xwininfo可以进行-id切换,以便您可以向他们提供您感兴趣的窗口 ID,而不必单击它:

$ xprop -id 0x5c00001|grep -i class
WM_CLASS(STRING) = "gnome-terminal", "Gnome-terminal"
Run Code Online (Sandbox Code Playgroud)

参考