Skip to content

Instantly share code, notes, and snippets.

@thor27
Last active November 11, 2016 16:46
Show Gist options
  • Save thor27/89881487e986814195d84b035a80793f to your computer and use it in GitHub Desktop.
Save thor27/89881487e986814195d84b035a80793f to your computer and use it in GitHub Desktop.
Raise all windows of the active one. Bind this to a shortcut (such as SUPER + UP) and when you press all windows of this app goes foreground. With a special hack for CSSH. Taken from here with updates: http://askubuntu.com/questions/446521/how-to-show-all-windows-of-an-application/
#!/usr/bin/env python3
import subprocess
import getpass
import time
def get(command):
return subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
def execute(command):
subprocess.Popen(["/bin/bash", "-c", command])
def get_frontmost():
cmd = "xprop -root"
frontmost = [l for l in get(cmd).splitlines() if\
"ACTIVE_WINDOW(WINDOW)" in l][0].split()[-1]
first_part = frontmost[:2]
last_part = frontmost[2:]
return first_part + '0' * (8 - len(last_part)) + last_part
def get_full_data(text, w_data):
for l in w_data:
if text in l:
return l.split()
# calculate screen resolution
res_output = get("xrandr").split(); idf = res_output.index("current")
res = (int(res_output[idf+1]), int(res_output[idf+3].replace(",", "")))
# get window data for various purposes
w_data = get("wmctrl -lpG").splitlines()
non_windows = sum([[l.split()[0] for l in w_data if it in l]\
for it in ("unity-launcher", "unity-panel", "unity-dash", "Hud")], [])
# get id of current window
curr_window = get_frontmost()
# user gets 3 seconds to pick an application window (or launcher icon)
t = 0
# while t < 4:
# w_id1 = get_frontmost()
# time.sleep(1)
# w_id2 = get_frontmost()
# if w_id1 == w_id2 or w_id2 in non_windows+[curr_window]:
# t = t+1
# else:
# new_frontmost = w_id2
# break
new_frontmost = get_frontmost()
# raise
try:
full_data = get_full_data(new_frontmost, w_data)
if full_data[8] in ['CSSH', 'CSSH:']:
pids = [l.split()[2] for l in w_data if 'CSSH' in l.split()[8] in ['CSSH', 'CSSH:']]
else:
group = full_data[5]
pids = [l.split()[2] for l in w_data if l.split()[5] == group]
wl_data = [l.split() for l in w_data]
raise_windows = [l[0] for l in wl_data if l[2] in pids and\
0 < int(l[3]) < res[0] and 0 < int(l[4]) < res[1]]
[execute("wmctrl -ia "+item) for item in raise_windows]
except NameError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment