6

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?

Misho21
  • 437

2 Answers2

6

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.

Jacob Vlijm
  • 83,767
4

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

Paul B
  • 434
  • great. I worked a bit on your script to make it shorter. but you have or [We ;)] have a bug In our script: It capture only firefox because you only get the $3 of first browser. you need to store all $3 of all browsers into an array and check inside an if condition when one of them goes to maximize. +1 to your script specially for finding value 65 for maximized window ;) – αғsнιη Nov 25 '14 at 18:46
  • Very nice! only thing is that it also hides the launcher on workspaces where there is no maximaized window (if there is one one of the workspaces. – Jacob Vlijm Nov 25 '14 at 19:08
  • --- and it works unpredictably sometimes, I have it running, nowhere a maximized window, in workspace 2/4 it autohides? – Jacob Vlijm Nov 25 '14 at 19:34
  • Thank you very much! Very nice script KasiyA. I've grep unity-launcher because if you change unity launcher icon size script will not work with value 65 so that's why i grep it. About $3, I never seen people who use two different browsers at the same time (maybe except tor) :P but yeah I agree that is the work for bigger script. – Paul B Nov 25 '14 at 19:57
  • Jacob, maybe its because when you open new browser window(or something), position of that new window goes to 65 by default. Even if you grab and move your browser by your hand to position 65 on screen launcher will hide. :P I don't know how to deal with workspaces, I don't even use them. That workspace thingy sounds like a hard work for me to do. – Paul B Nov 25 '14 at 20:09
  • @PaulB 'm 65 is not the value for maximized window. That is the position of left side of window. nice ;) – αғsнιη Nov 25 '14 at 20:43
  • @PaulB Also if I move/drag the broswer window out to left side. launcher goes to hide mode. Because you coded it here if [ $a -lt $b ];then eval $hide – αғsнιη Nov 25 '14 at 21:01
  • @KasiyA yes I know what 65 value is. If you change icon size value must be different because launcher becomes smaller or bigger. And yes $a -lt $b was what I want, you know like those autohides in cairo-dock or docky when you move window to them they hide - maybe not great idea but I want it to try and maybe I'll improve script tomorrow just for fun. Thanks for tips :) – Paul B Nov 25 '14 at 21:38
  • this also does the job nicely! wonder which one to use this one or the python script ?! – Misho21 Nov 27 '14 at 14:33
  • Use which one you prefer. ;) I update my script to support workspaces. I don't know why but for me Jacob's python script lagging my system even mouse cursor every 2 seconds while running. – Paul B Nov 27 '14 at 15:35
  • you two did awsome job thank you.. didn't noctice any lag with jacob's script – Misho21 Nov 27 '14 at 17:10