John Floren

Home | Blog
Back to blog archive

Posted 2018/4/6

A script for moving offscreen windows in rio

I’ve started using Plan9Port’s rio window manager again on my laptop. I usually connect the laptop to a higher-resolution external monitor, which means when I disconnect the monitor, windows at the bottom / on the right edge of the screen may no longer be visible.

I wrote a quick script, xs, to re-jigger these windows back onto the screen. It uses both xshove (included with p9p) and xdotool, because I found that xshove very kindly lists out all windows on the current screen, while xdotool is better at moving windows by window ID rather than name. It will move any windows outside the bounds of the current resolution a randomized distance inside the viewport (so multiple windows don’t end up all stacked exactly on top of each other)

#!/bin/bash
set -e
screenwidth=`xrandr | awk '/current/{print $8}'`
screenheight=`xrandr | awk '/current/{print $10}' | sed -e 's/,//g'`

xshove | while read l
do
	pos=`echo $l | awk '{print $2}'`
	x=`echo $pos | awk -F "," '{print $1}'`
	y=`echo $pos | awk -F "," '{print $2}'`
	winid=`echo $l | awk '{print $1}'`

	echo $winid : x = $x , y = $y
	if [ "$x" -gt `expr "$screenwidth" + 50` ]
	then
		rand=`expr $RANDOM % 100 + 50`
		xdotool windowmove 0x$winid `expr $screenwidth - $rand` y
	fi

	if [ "$y" -gt `expr "$screenheight" + 50` ]
	then
		rand=`expr $RANDOM % 100 + 50`
		xdotool windowmove 0x$winid x `expr $screenheight - $rand`
	fi
done