当我最大化浏览器时,如何通过脚本自动隐藏启动器?

当我最大化浏览器时,如何通过脚本自动隐藏启动器?

我试图控制 Ubuntu 14.4.1 Launcher 的行为。我希望每次打开浏览器窗口时它都能自动隐藏,就像 Firefox maxmaized 一样。我找到了这个解决方案:

#!/bin/bash

## Change value of "hide" to the command which worked for you to hide the panel
hide='gsettings set com.canonical.Unity2d.Launcher hide-mode 1;'

## Change value of "show" to the command which worked for you to show the panel when it was hidden
show='gsettings set com.canonical.Unity2d.Launcher hide-mode 0;'

## Look for the grep value, add a new browser or application name followed by "\|" eg: 'firefox\|google\|chromium'
while [ 1 ]
 do z=$(wmctrl -l -p | grep -i 'firefox\|google');
    if [ -n "$z" ]; then 
        eval $hide
    else
        eval $show
    fi;
    sleep 2;
done;

但似乎太旧了,无法工作,然后我发现

我尝试将这两个脚本合并在一起,因此我所做的如下:

#!/bin/bash

AUTOHIDE=$(dconf read /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode)
if [[ $AUTOHIDE -eq 1 ]]
then
     dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode 0
else
     dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode 1
fi

## Look for the grep value, add a new browser or application name followed by "\|" eg: 'firefox\|google\|chromium'
while [ 1 ]
 do z=$(wmctrl -l -p | grep -i 'firefox\|google');
    if [ -n "$z" ]; then 
        eval $hide
    else
        eval $show
    fi;
    sleep 2;
done;

但是脚本不起作用。有人可以为我改进这个脚本并使其正常工作吗?

答案1

下面是两个版本的脚本,用于在应用程序窗口最大化时自动隐藏启动器。这些脚本已在 14.04 / 14.10 /16.04 上测试

差异

  • 第一个版本是“通用”版本,它使启动器在窗口打开时自动隐藏任何应用程序已最大化。
  • 第二个使启动器自动隐藏,但仅限于您在脚本的头部部分中特别定义的应用程序。

这两个脚本都可以识别要图标化的窗口,因此没有理由自动隐藏,并且这两个脚本都适用于特定工作区;启动器仅在实际存在一个或多个窗口的工作区上切换到自动隐藏最大化。

安装 wmctrl

该脚本用于wmctrl映射当前打开的窗口。您可能需要安装它:

sudo apt-get install wmctrl

脚本


下面两个脚本均于 2017 年 3 月更新/重写。


1.“基础”版本,作用于所有应用程序的最大化窗口

#!/usr/bin/env python3
import subprocess
import time

mx = "_NET_WM_STATE_MAXIMIZED_VERT, _NET_WM_STATE_MAXIMIZED_HORZ"
key = ["org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/",
       "launcher-hide-mode"]

def get(cmd):
    try:
        return subprocess.check_output(cmd).decode("utf-8").strip()
    except subprocess.CalledProcessError:
        pass

def force_get(cmd):
    # both xprop and wmctrl break once and a while, this is to retry if so
    val = None
    while not val:
        val = get(cmd)
    return val

def get_res():
    # look up screen resolution
    scrdata = get("xrandr").split(); resindex = scrdata.index("connected")+2
    return [int(n) for n in scrdata[resindex].split("+")[0].split("x")]

res = get_res()
hide1 = False

while True:
    time.sleep(2)
    hide = False
    wlist = [l.split() for l in force_get(["wmctrl", "-lpG"]).splitlines()]
    # only check windows if any of the apps is running
    for w in wlist:
        xpr = force_get(["xprop", "-id", w[0]])
        if all([
            mx in xpr, not "Iconic" in xpr,
            0 <= int(w[3]) < res[0], 0 <= int(w[4]) < res[1],
            ]):
            hide = True
            break
    if hide != hide1:
        nexts = "0" if hide == False else "1"
        currset = get(["gsettings", "get", key[0], key[1]])
        if nexts != currset:
            subprocess.Popen([
            "gsettings", "set", key[0], key[1], nexts
            ])
    hide1 = hide

2. 应用程序特定版本:

#!/usr/bin/env python3
import subprocess
import time

apps = ["gnome-terminal", "firefox"]
mx = "_NET_WM_STATE_MAXIMIZED_VERT, _NET_WM_STATE_MAXIMIZED_HORZ"
key = ["org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/",
       "launcher-hide-mode"]

def get(cmd):
    try:
        return subprocess.check_output(cmd).decode("utf-8").strip()
    except subprocess.CalledProcessError:
        pass

def force_get(cmd):
    # both xprop and wmctrl break once and a while, this is to retry if so
    val = None
    while not val:
        val = get(cmd)
    return val

def get_res():
    # look up screen resolution
    scrdata = get("xrandr").split(); resindex = scrdata.index("connected")+2
    return [int(n) for n in scrdata[resindex].split("+")[0].split("x")]

res = get_res()
hide1 = False

while True:
    time.sleep(2)
    hide = False
    wlist = [l.split() for l in force_get(["wmctrl", "-lpG"]).splitlines()]
    pids = [get(["pgrep", app]) for app in apps]
    # only check windows if any of the apps is running
    if any(pids):
        for w in wlist:
            xpr = force_get(["xprop", "-id", w[0]])
            if all([
                mx in xpr, not "Iconic" in xpr,
                0 <= int(w[3]) < res[0], 0 <= int(w[4]) < res[1],
                any([w[2] == pid for pid in pids]),
                ]):
                hide = True
                break
        if hide != hide1:
            nexts = "0" if hide == False else "1"
            currset = get(["gsettings", "get", key[0], key[1]])
            if nexts != currset:
                subprocess.Popen([
                "gsettings", "set", key[0], key[1], nexts
                ])
        hide1 = hide

如何使用:

将其中一个脚本复制到一个空文件中,
[设置,如果选择第二个,则隐藏您的应用程序]
并将其保存为autohide.py

通过命令运行:

python3 /path/to/autohide.py

如果它按照您想要的方式运行,请将其添加到您的启动应用程序中。
注意:如果您将其用作启动应用程序,则应取消注释该行:

time.sleep(10)

在脚本的头部。如果在桌面完全加载之前调用该脚本,则脚本可能会崩溃。根据您的系统更改值 (10)。

解释

循环执行脚本:

  • [检查所设置应用程序的可能 pid]
  • 检查屏幕的分辨率,查看窗口的位置(相对于当前工作区)
  • 创建当前窗口及其状态的列表
  • 检查当前隐藏模式(0 表示不自动隐藏,1 表示自动隐藏)

(仅)如果需要更改隐藏模式,脚本才会更改设置。

答案2

大家开始吧。在我的 Ubuntu 14.04 上使用原始 Unity 环境进行了测试。希望有人欣赏我的小小作品……

适用于一个浏览器窗口

#!/bin/bash
## Tested with Ubuntu 14.04 Unity
## Auto hide Unity Launcher when web browser is maximized 
## wmctrl is required: sudo apt-get install wmctrl
## ~pba


## Change value of "key" to the command which worked for you
key='gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ launcher-hide-mode';

while [ 1 ];
 do
 p=$(wmctrl -lG);
 a=($(echo -E "$p" | grep -i "unity-launcher"));
 w=($(echo -E "$p" | grep -i "firefox\|google\|chromium\|opera"));
 if [ ${w[0]} ]; then
 e=$(xwininfo -all -id ${w[0]});
 l=( $(echo -E "$e" | grep -ci '   Hidden')
     $(echo -E "$e" | grep -ci '   Maximized Vert')
     $(echo -E "$e" | grep -ci '   Maximized Horz') );
 b=($(echo -E "$p" | grep -i "unity-panel"));
 if [ ${l[0]} -ne "1" -a ${l[1]} -eq "1" -a ${l[2]} -eq "1" -a ${w[2]} -eq ${a[4]} -a ${w[3]} -eq ${b[5]} ]; then 
  eval "$key 1"; 
   elif [ ${l[0]} -ne "1" -a ${l[1]} -ne "1" -a ${l[2]} -ne "1" -a ${a[3]} -lt "0" ]; then 
    eval "$key 0";
   elif [ ${l[0]} -eq "1" -a ${a[3]} -lt "0" -a ${w[2]} -ne "1" ]; then 
    eval "$key 0";
   elif [ ${l[0]} -ne "1" -a ${l[1]} -eq "1" -a ${l[2]} -eq "1" -a ${a[3]} -lt "0" -a ${w[2]} -ne "0" ]; then 
    eval "$key 0";
   elif [ ${l[0]} -ne "1" -a ${l[1]} -eq "1" -a ${l[2]} -eq "1" -a ${a[3]} -lt "0" -a ${w[3]} -ne ${b[5]} -a ${w[3]} -ne "0" ]; then 
    eval "$key 0";
 fi;
 elif [ ${a[3]} -lt "0" ]; then eval "$key 0";
 fi;
 sleep 2;
done;

较旧脚本

相关内容