How can I cycle windows in taskbar order?
Cycle through windows from left to right
(either on the current viewport only, or across all viewports)
The script below, added to a shortcut key, will cycle through your windows from left to right:
The script
#!/usr/bin/env python3
import subprocess
from operator import itemgetter
import sys
this_ws = True if "oncurrent" in sys.argv[1:] else False
nxt = -1 if "backward" in sys.argv[1:] else 1
def get(command):
try:
return subprocess.check_output(command).decode("utf-8").strip()
except subprocess.CalledProcessError:
pass
def exclude(w_id):
# exclude window types; you wouldn't want to incude the launcher or Dash
exclude = ["_NET_WM_WINDOW_TYPE_DOCK", "_NET_WM_WINDOW_TYPE_DESKTOP"]
wdata = get(["xprop", "-id", w_id])
return any([tpe in wdata for tpe in exclude])
if this_ws:
# if only windows on this viewport should be picked: get the viewport size
resdata = get("xrandr").split(); index = resdata.index("current")
res = [int(n.strip(",")) for n in [resdata[index+1], resdata[index+3]]]
# get the window list, geometry
valid = [w for w in sorted([[w[0], int(w[2]), int(w[3])] for w in [
l.split() for l in get(["wmctrl", "-lG"]).splitlines()
]], key = itemgetter(1)) if not exclude(w[0])]
# exclude windows on other viewports (if set)
if this_ws:
valid = [w[0] for w in valid if all([
0 <= w[1] < res[0], 0 <= w[2] < res[1]
])]
else:
valid = [w[0] for w in valid]
# get active window
front = [l.split()[-1] for l in get(["xprop", "-root"]).splitlines() if \
"_NET_ACTIVE_WINDOW(WINDOW)" in l][0]
# convert xprop- format for window id to wmctrl format
current = front[:2]+((10-len(front))*"0")+front[2:]
# pick the next window
try:
next_win = valid[valid.index(current)+nxt]
except IndexError:
next_win = valid[0]
# raise next in row
subprocess.Popen(["wmctrl", "-ia", next_win])
How to use
-
The script needs wmctrl
sudo apt-get install wmctrl
Copy the script into an empty file, save it as
cyclewins.py
-
Add the script to a shortcut key: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
python3 /path/to/cyclewins.py
That's it
Only cycle through windows on current viewport?
In case you only want to cycle through windows on the current viewport, the the script with the argument oncurrent
:
python3 /path/to/cyclewins.py oncurrent
Cycle backward?
In case you would like to cycle from right to left, run the script with the argument backward
:
python3 /path/to/cyclewins.py backward
Of course the combination of the two arguments is possible;
python3 /path/to/cyclewins.py backward oncurrent
will cycle backward through your windows on the current viewport.