Handful of Bash Scripts for Window Positioning Using wmctrl


Centre window (with a bit of randomness, so it doesn’t perfectly overlap)
centre-window.sh

#!/bin/bash
# Thanks:
# http://superuser.com/questions/196532/how-do-i-find-out-my-screen-resolution-from-a-shell-script
# http://superuser.com/questions/442474/how-do-i-unmaximize-a-window-with-xdotool-or-similar-by-command-line
Xaxis=$(xrandr --current | grep '*+' | uniq | awk '{print $1}' |  cut -d 'x' -f1)
Yaxis=$(xrandr --current | grep '*+' | uniq | awk '{print $1}' |  cut -d 'x' -f2)
WIDTH=$(echo "scale=0;$Xaxis*.65"|bc)
HEIGHT=$Yaxis
X=$(echo "scale=0;$Xaxis/2-$WIDTH/2"|bc)
Y=0 
RAND1=`grep -m1 -ao '[0-9]' /dev/urandom | sed s/0/100/ | head -n1`
RAND2=`grep -m1 -ao '[0-9]' /dev/urandom | sed s/0/100/ | head -n1`
X=$((RANDOM%100+1900))
Y=0
WIDTH=$((RANDOM%200+1200))
HEIGHT=1080
WIN=`xdotool getactivewindow`
echo $Xaxis
echo $Yaxis
echo $WIDTH x $HEIGHT
echo $X x $Y
# unmaximize
wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz
xdotool windowmove $WIN $X $Y
xdotool windowsize $WIN $WIDTH $HEIGHT

Two monitor setup. I have a laptop screen (1600×900) left of my main monitor (1920×1280). Change accordingly!

move-to-left-monitor.sh

#!/bin/bash
wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz
wmctrl -r :ACTIVE: -e 0,0,-1,-1,-1

move-to-right-monitor.sh

#!/bin/bash
wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz
wmctrl -r :ACTIVE: -e 0,2000,-1,-1,-1