Skip to content

Instantly share code, notes, and snippets.

@sephamorr
Last active Sep 16, 2020
Embed
What would you like to do?
#!/usr/bin/python
#You may have to change some of these values to match display/dock/menubar sizes
xscr=1920
yscr=1080
yd=5
xd=80
xstart=0
ystart=44
xmax=xscr-xd
ymax=yscr-yd
import subprocess,os
output = subprocess.Popen(["wmctrl", "-l","-G"], stdout=subprocess.PIPE).communicate()[0].split("\n")
class win():
def __init__(self, wid, desktop, x, y, w, h, name):
self.wid = wid
self.desktop = int(desktop)
self.x = int(x)
self.y = int(y)
self.w = int(w)
self.h = int(h)
self.name = name
def __str__(self):
return __repr__(self)
def __repr__(self):
return "%s (%s): (%d,%d) @ (%d,%d):%d"%(self.name,self.wid,self.w,self.h,self.x,self.y,self.desktop)
wins=[]
for i in output:
f=filter(None,i.split(" "))
if(len(f)<7):
continue
wins.append(win(f[0], f[1], f[2], f[3], f[4], f[5]," ".join(f[6:])))
for i in wins:
if i.x<0 or i.x>xmax or i.y<0 or i.y>ymax:
w=min(i.w,xmax-40)
h=min(i.h,ymax)
x=0
y=0
elif (i.x+i.w)>xscr or (i.y+i.h)>yscr:
x=i.x-xstart
y=i.y-ystart
w=xscr-x-xstart
h=yscr-y-ystart
else:
continue
cmd="wmctrl -i -r %s -e 0,%d,%d,%d,%d"%(i.wid,x,y,w,h)
os.system(cmd)
@mhaghdoost
Copy link

mhaghdoost commented Sep 19, 2017

thank you very much! it works like a charm 👍

@stevekm
Copy link

stevekm commented Oct 4, 2017

wow it works thanks

@vivekkohar
Copy link

vivekkohar commented Oct 5, 2017

I get an error in this line:
output = subprocess.Popen(["wmctrl", "-l","-G"], stdout=subprocess.PIPE).communicate()[0].split("\n")
TypeError: a bytes-like object is required, not 'str'

Any ideas?

@sbliven
Copy link

sbliven commented Dec 4, 2017

@vivekkohar Use python2

@AugustM
Copy link

AugustM commented Mar 14, 2019

I'm on a Mac running Mojave, Python 2.7.
When I run wmfix I get:

$ /usr/local/bin/wmfix
Traceback (most recent call last):
  File "/usr/local/bin/wmfix", line 15, in <module>
    output = subprocess.Popen(["wmctrl", "-l","-G"], stdout=subprocess.PIPE).communicate()[0].split("\n")
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

The message about line 15 in wmfix, is it trying to say there's something wrong with that line? There's no error message. Or is it just saying that that is line that calls subprocess.py?

I can find my way into the subprocess.py file and see lines 710 and 1335, but I'm not enough of a Python programmer to make sense of it from there.

Any suggestions?

@schallee
Copy link

schallee commented Sep 16, 2020

This runs wmctrl which you will need to install for it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment