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.