如何创建快捷方式来启动、最小化和将程序置于前台

如何创建快捷方式来启动、最小化和将程序置于前台

我刚刚切换到 Linux,正在设置我的笔记本电脑。在 Windows 中,我使用 AutoHotkey 快捷方式执行以下操作:
- 在“chrome”未处于活动状态时打开它。-
在“chrome”处于前台时将其最小化。-
在窗口未处于活动状态时将其置于前台。

我在“Autokey”中使用了以下代码,效果很好,但如果我可以使用简单的 bash 脚本来完成此操作,我会很高兴:

#AutoKey script to toggle any windowed application, Nautilus as the example. Requires xdotool and wmctrl.
import subprocess
command = 'wmctrl -lx'
output = system.exec_command(command, getOutput=True)  

if 'google-chrome' in output:
    winClass = window.get_active_class()
    if winClass == 'google-chrome.Google-chrome':
        system.exec_command("xdotool windowminimize $(xdotool getactivewindow)")
    else:
        system.exec_command("wmctrl -x -a google-chrome")

else:
    system.exec_command("google-chrome")
#end script

现在我尝试将其转换为 bash 代码,但我在检查 chrome 是否在前台运行时卡住了。我想到了以下伪代码,但找不到正确的 shell 命令:

if chrome is not open
   open chrome
else
   if chrome is on foreground
      minimize chrome
   else
      bring chrome to foreground
end

我尝试在 Linux 中使用 Linux“快捷方式”应用程序中的 shell 脚本来实现此行为。但到目前为止,我还没有找到一种方法来检查 Google-chrome 是否在前台。我尝试使用“xdotools”包,但这似乎不起作用:

if ($(xdotool search --name "Google Chrome") -eq $(xdotool getactivewindow))
     xdotool windowminimize $(xdotool getactivewindow)
else
     wmctrl -x -a google-chrome
end

您是否对实现此目标的最佳方法有一些建议?我现在有以下原料:

要打开 google-chrome,我使用:

google-chrome &

为了最小化 google-chrome 我使用:

xdotool windowminimize $(xdotool getactivewindow)

为了最大限度地利用 Google Chrome,我使用:

wmctrl -x -a google-chrome

我想我需要使用这样的东西来检查 chrome 是否在前台:

wmctrl -lx
xdotool search --name "Google Chrome"
xdotool getactivewindow
enter code here

提前谢谢您,
您好,里克,

系统信息:台式机:Hp Zbook G3 studio 分销商ID:Ubuntu 描述:Ubuntu Bionic Beaver(开发分支) 发行版本:18.04 代号:bionic

答案1

我收到了来自一位用户的回答另一个论坛

#!/bin/bash

chromefocus=$(xdotool getwindowfocus getwindowname | grep -c "Google Chrome")

if [ "$chromefocus" -gt "0" ]; then
        xdotool windowminimize $(xdotool getactivewindow)
    else
        wmctrl -xa "google-chrome.Google-chrome" || /usr/bin/google-chrome
fi

相关内容