Skip to content

Instantly share code, notes, and snippets.

@uogbuji
Created January 11, 2017 01:50
Show Gist options
  • Save uogbuji/9615732450d4bf31627c5175205f3fda to your computer and use it in GitHub Desktop.
Save uogbuji/9615732450d4bf31627c5175205f3fda to your computer and use it in GitHub Desktop.
Linux tool to move all windows from one monitor to another in a multi-monitor config
#!/usr/bin/env python3
#Based on script 2 from http://askubuntu.com/questions/597884/swap-windows-between-screens-in-dual-screen-mode/598038#598038
import subprocess
import sys
vec = sys.argv[1]
def get(cmd):
return subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
def get_shiftright(xr_output):
lines = [l for l in xr_output.splitlines() if "+0+0" in l][0].split()
return int([it for it in lines if "x" in it][0].split("x")[0])
def get_shiftleft(xr_output):
lines = [l for l in xr_output.splitlines() if "+0" in l and not "+0+0" in l][0].split()
return -int([it for it in lines if "x" in it][0].split("x")[0])
def shift_windows():
xr_output = get("xrandr")
shift_r = get_shiftright(xr_output)
shift_l = get_shiftleft(xr_output)
w_data = [l.split() for l in get("wmctrl -lG").splitlines()]
for w in w_data:
props = get("xprop -id "+w[0])
if vec == "right":
check = (0 < int(w[2]) < shift_r, "_TYPE_NORMAL" in props, "TYPE_DIALOG" in props).count(True)
shift = shift_r
if vec == "left":
check = (shift_r-shift_l > int(w[2]) >= shift_r , "_TYPE_NORMAL" in props, "TYPE_DIALOG" in props).count(True)
shift = -shift_r
if check == 2:
command = "wmctrl -ir "+w[0]+" -e 0,"+(",").join([str(int(w[2])+shift), str(int(w[3])-28), w[4], w[5]])
subprocess.Popen(["/bin/bash", "-c", command])
shift_windows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment