Skip to content

Instantly share code, notes, and snippets.

@stiltr
Created February 21, 2017 22:51
Show Gist options
  • Save stiltr/e65a29de521a43b91c3462ef2ef7cbf0 to your computer and use it in GitHub Desktop.
Save stiltr/e65a29de521a43b91c3462ef2ef7cbf0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Moves the mouse to the next monitor (R or L)
# If xdotool getwindowgeometry --shell `xdotool getactivewindow` reports
# a value other than 0,0 when your window is in the top left corner, use
# the values it shows for the offsets. Some tweaking may be required.
#x_offset=6
#y_offset=58
x_offset=0
y_offset=0
# Script defaults to moving to the right
# 1 => right, -1 => L
direction=1
# Check for arguments
if [ "$#" -gt 0 ]
then
if [ "$1" == "L" ]
then
direction=-1
fi
fi
# number of monitors
#use this command or hardcode
##num_monitors=`xrandr | grep " connected" | wc -l`
num_monitors=3
# Get monitor dimmesions from L to R and put into $monitors[$i]
#tmp=0
#monitors[0]=0
#for ((i=0; i<$num_monitors; i++))
#do
# monitors[$i]=`xrandr | grep +$tmp+0 | cut -d" " -f3`
# let "tmp+=`echo ${monitors[$i]} | cut -d"x" -f1`"
#done
monitors[0]="1920x1080+0+0"
monitors[1]="1920x1080+1920+0"
monitors[2]="1680x1050+3840+0"
# Get mouse location
#x=`xwininfo -id $id | awk '/Absolute upper-left X:/ {print $4}'`
#mouseloc=`xdotool getmouselocation --shell`
x=`xdotool getmouselocation --shell 2>/dev/null | awk '/X=/' | sed 's/X=//'`
y=`xdotool getmouselocation --shell 2>/dev/null |awk '/Y=/' | sed 's/Y=//'`
if [ "$x" -lt 0 ]
then
x=0
fi
if [ "$y" -lt 0 ]
then
y=0
fi
current_monitor=-1
for ((i=$num_monitors-1; i>=0; i--))
do
if [ "$x" -ge `echo ${monitors[$i]} | cut -d"+" -f2` ]
then
current_monitor=$i
break
fi
done
let "rel_x=$x-`echo ${monitors[$current_monitor]} | cut -d"+" -f2 `"
# Set the new monitor
((new_monitor=($current_monitor+$direction)%$num_monitors))
# Fix negative values
if [ "$new_monitor" -lt 0 ]
then
let "new_monitor+=$num_monitors"
fi
# Find new X value: relative x val + x_offset of new monitor
let "new_x = $rel_x - $x_offset + `echo ${monitors[$new_monitor]} | cut -d"+" -f2`"
let "new_y = $y - $y_offset"
# Move mouse to new coordinates
xdotool mousemove $new_x $new_y
#!/bin/bash
# Moves the currently focussed window to the next monitor (R or L)
# If xdotool getwindowgeometry --shell `xdotool getactivewindow` reports
# a value other than 0,0 when your window is in the top left corner, use
# the values it shows for the offsets. Some tweaking may be required.
x_offset=6
y_offset=58
#x_offset=0
#y_offset=0
# Script defaults to moving to the right
# 1 => right, -1 => L
direction=1
# Check for arguments
if [ "$#" -gt 0 ]
then
if [ "$1" == "L" ]
then
direction=-1
fi
fi
# number of monitors
num_monitors=`xrandr | grep " connected" | wc -l`
# Get monitor dimmesions from L to R and put into $monitors[$i]
tmp=0
monitors[0]=0
for ((i=0; i<$num_monitors; i++))
do
monitors[$i]=`xrandr | grep +$tmp+0 | cut -d" " -f3`
let "tmp+=`echo ${monitors[$i]} | cut -d"x" -f1`"
done
# Get active window ID
id=`xdotool getactivewindow`
# Is window maximized?
maximized=`xprop -id $id _NET_WM_STATE | awk '/MAXIMIZED/' | wc -l`
# If window is maximized, unmaximize it for transport
if [ "$maximized" -ne 0 ]
then
wmctrl -ir $id -b remove,maximized_vert,maximized_horz
fi
# Get active window location
#x=`xwininfo -id $id | awk '/Absolute upper-left X:/ {print $4}'`
x=`xdotool getwindowgeometry --shell $id | awk '/X=/' | sed 's/X=//'`
y=`xdotool getwindowgeometry --shell $id | awk '/Y=/' | sed 's/Y=//'`
if [ "$x" -lt 0 ]
then
x=0
fi
if [ "$y" -lt 0 ]
then
y=0
fi
current_monitor=-1
for ((i=$num_monitors-1; i>=0; i--))
do
if [ "$x" -ge `echo ${monitors[$i]} | cut -d"+" -f2` ]
then
current_monitor=$i
break
fi
done
let "rel_x=$x-`echo ${monitors[$current_monitor]} | cut -d"+" -f2 `"
# Set the new monitor
((new_monitor=($current_monitor+$direction)%$num_monitors))
# Fix negative values
if [ "$new_monitor" -lt 0 ]
then
let "new_monitor+=$num_monitors"
fi
# Find new X value: relative x val + x_offset of new monitor
let "new_x = $rel_x - $x_offset + `echo ${monitors[$new_monitor]} | cut -d"+" -f2`"
let "new_y = $y - $y_offset"
# Move window to new coordinates
#xdotool windowmove --sync $id $new_x $new_y
xdotool windowmove $id $new_x $new_y
# Finally, if it was originally maximized, maximize it
if [ "$maximized" -ne 0 ]
then
wmctrl -ir $id -b add,maximized_vert,maximized_horz
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment