How can I auto-hide the launcher via a script, when I maximize the browser?

I was trying to control Ubuntu 14.4.1 Launcher's behavior. I want it to auto-hide every time I have browser window like firefox maxmaized. I found this solution:

#!/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;

but is seems too old to work then I found this

I tried to combine the two scripts together so here is what I did:

#!/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;

But script doesn't work. can anybody refine this script to me and get it to work?


Solution 1:

Below two versions of a script to autohide the launcher when an application's window is maximized. The scripts are tested on 14.04 / 14.10 /16.04

The differences

  • The first version is a "general" version, it makes the launcher autohide whenever a window of any application is maximized.
  • The second one makes the launcher autohide, but only on applications that you specifically define in the headsection of the script.

Both scripts recognize windows to be iconized, then there is no reason to autohide, and both scripts work workspace- specific; the launcher only switches to autohide on workspaces where actually one or more windows are maximized.

Installing wmctrl

The scripts use wmctrl to map the currently opened windows. You might have to install it:

sudo apt-get install wmctrl

The scripts


Both scripts below were updated/rewritten March 2017.


1. The "basic" version, acts on maximized windows of all applications

#!/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. The application- specific version:

#!/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

How to use:

Copy either one of the scripts into an empty file,
[set, if you chose the second one, your applications to hide]
and save it as autohide.py.

Run it by the command:

python3 /path/to/autohide.py

If it acts like you want it to, add it to your startup applications.
N.B. If you use it as a startup application, you should uncomment the line:

time.sleep(10)

In the head section of the script. The script might crash if it is called before the desktop is fully loaded. Change the value (10), depending on your system.

Explanation

In a loop the script:

  • [checks the possible pids of the set applications]
  • checks the screen's resolution, to see where the windows are positiond (relative to the current workspace)
  • creates a list of current windows, their state
  • checks the current hide-mode (either 0 for not- autohide or 1 for autohide)

(only) if a change in the hide-mode needs to be made, the script changes the setting.

Solution 2:

Here you go guys. Tested on my Ubuntu 14.04 with original Unity environment. Hope someone appreciate my little work...

It's suitable for one browser window

#!/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;

Older script