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?

5

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.

  • Comments are not for extended discussion; this conversation has been moved to chat. – fossfreedom Dec 5 '14 at 22:18
  • don't know about you jacob but the new script really stops many times. I am confused – Misho21 Dec 8 '14 at 15:55
  • @Misho21 Not a single error in 24 hours, but let's discuss it on chat... – Jacob Vlijm Dec 8 '14 at 16:35
  • @Misho21 28 hours and still no error... But this fix should catch all possible errors. – Jacob Vlijm Dec 8 '14 at 20:50
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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.