Skip to content

Instantly share code, notes, and snippets.

@pymen
Created March 4, 2015 17:12
Show Gist options
  • Save pymen/d3e5f9e8116d55956cd7 to your computer and use it in GitHub Desktop.
Save pymen/d3e5f9e8116d55956cd7 to your computer and use it in GitHub Desktop.
Script to move window to opposite monitor
#!/bin/bash
#++++++++++++++++
# Monitor Switch
#
# Moves currently focused window from one monitor to the other.
# Designed for a system with two monitors.
# Script should be triggered using a keyboard shortcut.
# If the window is maximized it should remain maximized after being moved.
# If the window is not maximized it should retain its current size, unless
# height is too large for the destination monitor, when it will be trimmed.
#++++++++++++++++
# resolution of left monitor
w_l_monitor=1920
h_l_monitor=1080
# resolution of right monitor
w_r_monitor=1920
h_r_monitor=1080
# window title bar height (default title bar height in Gnome)
h_tbar=24
f="/home/ant/debug.log"
echo "Start_script" > $f
# focus on active window
window=`xdotool getactivewindow`
max_state=`xprop -id $window _NET_WM_STATE`
wmctrl -ir $window -b remove,maximized_vert,maximized_horz
eval `xdotool getwindowgeometry --shell $window`
# get active window size and position
x=`xwininfo -id $window | grep "Absolute upper-left X" | awk '{print $4}'`
y=`xwininfo -id $window | grep "Absolute upper-left Y" | awk '{print $4}'`
w=`xwininfo -id $window | grep "Width" | awk '{print $2}'`
h=`xwininfo -id $window | grep "Height" | awk '{print $2}'`
echo "x=$x" >> $f
echo "y=$y" >> $f
echo "w=$w" >> $f
echo "h=$h" >> $f
echo "w_l_monitor=$w_l_monitor" >> $f
echo "w_r_monitor=$w_r_monitor" >> $f
# window on left monitor
if [ "$x" -lt "$w_l_monitor" ]; then
echo "LEFT w_l_monitor=$w_l_monitor" >> $f
new_x=$(($x+$w_l_monitor-1))
new_y=$(($y-$h_tbar))
else
echo "RIGHT w_r_monitor=$w_r_monitor" >> $f
new_x=$(($x-$w_l_monitor-1))
new_y=$(($y-$h_tbar))
fi
xdotool windowmove $window $new_x $new_y
echo "new_x=$new_x" >> $f
echo "new_y=$new_y" >> $f
if [ -z "${max_state/*_NET_WM_STATE_MAXIMIZED_*/}" ]; then
echo "SET MAX" >> $f
wmctrl -ir $window -b add,maximized_vert,maximized_horz
fi
xdotool windowraise $window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment