如何创建一个快捷方式来关闭同一个应用程序的所有窗口?

当我右键单击Unity启动器中的应用程序图标时,我可以按退出以关闭与该应用程序对应的所有窗口。 是否可以使用在活动窗口上操作的键盘快捷键(以及对应于同一应用程序的所有其他窗口)执行相同的操作?

我可以用xkill做类似的xkill ,但在那种情况下,我没有记住未保存文件的消息。

user72216的答案并不总是有效。 例如,如果我打开几个Okular(一个KDE PDF查看器)窗口,代码将不会关闭所有窗口,因为不同的窗口ID分配给窗口。 以下代码提取所有窗口ID并正常关闭它们:

 #!/usr/bin/env python3 import subprocess def get(cmd): return subprocess.check_output(cmd).decode("utf-8").strip() pid = get(["xdotool", "getactivewindow", "getwindowpid"]) # Identify the name of the application username = get(["users"]) jobs = get(["ps","-u",username]) jobname = "" for w in jobs.splitlines(): jobinfo = w.split() if pid == jobinfo[0]: jobname = jobinfo[-1] break # Get all pids that match the jobname pidlist = [] for w in jobs.splitlines(): jobinfo = w.split() if jobinfo[-1] == jobname: pidlist = pidlist + [jobinfo[0]] # Close all windows with having the pids wlist = get(["wmctrl", "-lp"]) for pid in pidlist: for w in wlist.splitlines(): if pid in w and not "Desktop" in w: print(w.split()[0]) subprocess.call(["wmctrl", "-ic", w.split()[0]]) 

如何使用快捷键安全地关闭gui应用程序的所有窗口

正确关闭应用程序窗口并确保不会丢失数据的最安全方法是使用wmctrl (默认情况下不安装):

 wmctrl -ic  

要在脚本中使用它来关闭应用程序的所有窗口:

  1. 安装xdotoolwmctrl

     sudo apt-get install wmctrl xdotool 
  2. 将下面的脚本复制到一个空文件中,将其另存为stop_active.py

     #!/usr/bin/env python3 import subprocess def get(cmd): return subprocess.check_output(cmd).decode("utf-8").strip() pid = get(["xdotool", "getactivewindow", "getwindowpid"]) for w in get(["wmctrl", "-lp"]).splitlines(): if pid in w and not "Desktop" in w: subprocess.call(["wmctrl", "-ic", w.split()[0]]) 
  3. 将以下命令添加到快捷键:

     python3 /path/to/stop_active.py 

    选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。 单击“+”并添加命令:

     python3 /path/to/stop_active.py 

    NB不要在快捷键中使用~$HOME ,而是使用绝对路径。

现在,快捷方式可用于优雅地关闭最前面窗口的所有窗口。

说明

我尝试了各种kill选项( kill -2kill -HUPkill -s TERM 等),在几篇文章中提到了优雅地关闭应用程序。 在未保存的更改的gedit窗口上测试,所有这些都快乐地关闭窗口,没有任何交互。

wmctrl 确实询问该怎么做,类似于Ctrl + Q.

然后,该脚本首先使用以下命令找出最前面窗口的pid

 xdotool getactivewindow getwindowpid 

随后,使用以下命令调用当前打开的窗口列表:

 wmctrl -lp 

从该列表中,使用以下命令选择并关闭相应的窗口:

 wmctrl -ic  

注意

如果要关闭所有nautilus窗口,请在行中

 if pid in w and not "Desktop" in w: 

"Desktop"指的是您的桌面窗口,通常应始终保留。 如果您没有使用英文版的Ubuntu,请用您所用语言的桌面本地化名称替换"Desktop"

介绍

下面的脚本会杀死用户正在使用的当前活动窗口的所有活动窗口。这意味着绑定到快捷方式。

该脚本将显示一个弹出窗口,提示用户在杀死所有窗口之前进行确认。

该脚本使用所有本机(预安装)工具,例如qdbuszenitybash

获取脚本

您可以在此处复制脚本源,也可以使用下面的说明从我的git存储库中获取脚本源

  1. sudo apt-get install git
  2. cd /opt ; sudo git clone https://github.com/SergKolo/sergrep.git
  3. 该文件将位于/opt/sergrep/kill_windows_set.sh ; 使用sudo chmod +x kill_windows_set.sh确保文件可执行

将脚本绑定到快捷方式

相关信息可在此处找到:

如何将.sh文件绑定到键盘组合?

资源

 #!/usr/bin/env bash # ########################################################### # Author: Serg Kolo , contact: 1047481448@qq.com # Date: April 2nd , 2016 # Purpose: Close all windows of the active application # Written for: https://askubuntu.com/q/753033/295286 # Tested on: Ubuntu 14.04 LTS ########################################################### # Copyright: Serg Kolo , 2016 # # Permission to use, copy, modify, and distribute this software is hereby granted # without fee, provided that the copyright notice above and this permission statement # appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. ARGV0="$0" ARGC=$# get_running_apps() { qdbus org.ayatana.bamf /org/ayatana/bamf/matcher org.ayatana.bamf.matcher.RunningApplications } list_children() { qdbus org.ayatana.bamf "$1" org.ayatana.bamf.view.Children } get_pid() { qdbus org.ayatana.bamf "$1" org.ayatana.bamf.window.GetPid } main() { local ACTIVE local apps_list apps_list=( $( get_running_apps | tr '\n' ' ' ) ) for app in ${apps_list[@]} ; do ACTIVE=$(qdbus org.ayatana.bamf $app org.ayatana.bamf.view.IsActive) if [ "x$ACTIVE" = "xtrue" ] ; then windows=( $( list_children $app | tr '\n' ' ' ) ) fi done for window in ${windows[@]} ; do PIDS+=( $(get_pid $window) ) done if zenity --question \ --text="Do you really want to kill ${#PIDS[@]} windows ?" ; then kill ${PIDS[@]} fi } main 

脚本在行动

在此处输入图像描述

您可以为xkill创建快捷方式。

设置<键盘<快捷方式<自定义快捷方式

添加自定义快捷方式,然后只需在命令菜单中编写“xkill”,然后按所需的键设置自己的快捷方式。

就像你说的那样,虽然没有给你一个“保存未保存的文件”的消息。