bashScripts

Some random scripts for specific daily tasks.
git clone git://git.thepablogq.xyz/bashScripts
Log | Files | Refs

nxt_mon (2899B)


      1 #!/bin/sh
      2 #
      3 # Move the current window to the next monitor.
      4 #
      5 # Also works only on one X screen (which is the most common case).
      6 #
      7 # Props to
      8 # http://icyrock.com/blog/2012/05/xubuntu-moving-windows-between-monitors/
      9 #
     10 # Unfortunately, both "xdotool getwindowgeometry --shell $window_id" and
     11 # checking "-geometry" of "xwininfo -id $window_id" are not sufficient, as
     12 # the first command does not respect panel/decoration offsets and the second
     13 # will sometimes give a "-0-0" geometry. This is why we resort to "xwininfo".
     14 
     15 screen_width=$(xdpyinfo | awk '/dimensions:/ { print $2; exit }' | cut -d"x" -f1)
     16 screen_height=$(xdpyinfo | awk '/dimensions:/ { print $2; exit }' | cut -d"x" -f2)
     17 display_width=$(xdotool getdisplaygeometry | cut -d" " -f1)
     18 display_height=$(xdotool getdisplaygeometry | cut -d" " -f2)
     19 window_id=$(xdotool getactivewindow)
     20 
     21 # Remember if it was maximized.
     22 window_horz_maxed=$(xprop -id "$window_id" _NET_WM_STATE | grep '_NET_WM_STATE_MAXIMIZED_HORZ')
     23 window_vert_maxed=$(xprop -id "$window_id" _NET_WM_STATE | grep '_NET_WM_STATE_MAXIMIZED_VERT')
     24 
     25 # Un-maximize current window so that we can move it
     26 wmctrl -ir "$window_id" -b remove,maximized_vert,maximized_horz
     27 
     28 # Read window position
     29 x=$(xwininfo -id "$window_id" | awk '/Absolute upper-left X:/ { print $4 }')
     30 y=$(xwininfo -id "$window_id" | awk '/Absolute upper-left Y:/ { print $4 }')
     31 
     32 # Subtract any offsets caused by panels or window decorations
     33 x_offset=$(xwininfo -id "$window_id" | awk '/Relative upper-left X:/ { print $4 }')
     34 y_offset=$(xwininfo -id "$window_id" | awk '/Relative upper-left Y:/ { print $4 }')
     35 x=$(( x - x_offset))
     36 y=$(( y - y_offset))
     37 
     38 # Compute new X position
     39 new_x=$((x + display_width))
     40 # Compute new Y position
     41 new_y=$((y + display_height))
     42 
     43 # If we would move off the right-most monitor, we set it to the left one.
     44 # We also respect the window's width here: moving a window off more than half its width won't happen.
     45 width=$(xdotool getwindowgeometry "$window_id" | awk '/Geometry:/ { print $2 }'|cut -d"x" -f1)
     46 if [ "$(( new_x + width / 2))" -gt "$screen_width" ]; then
     47     new_x=$((new_x - screen_width))
     48 fi
     49 
     50 height=$(xdotool getwindowgeometry "$window_id" | awk '/Geometry:/ { print $2 }'|cut -d"x" -f2)
     51 if [ "$((new_y + height / 2))" -gt "$screen_height" ]; then
     52     new_y=$((new_y - screen_height))
     53 fi
     54 
     55 # Don't move off the left side.
     56 if [ "$new_x" -lt 0 ]; then
     57     new_x=0
     58 fi
     59 
     60 # Don't move off the bottom
     61 if [ "$new_y" -lt 0 ]; then
     62     new_y=0
     63 fi
     64 
     65 # Move the window
     66 xdotool windowmove "$window_id" "$new_x" "$new_y"
     67 
     68 # Maximize window again, if it was before
     69 if [ -n "${window_horz_maxed}" ] && [ -n "${window_vert_maxed}" ]; then
     70     wmctrl -ir "$window_id" -b add,maximized_vert,maximized_horz
     71 elif [ -n  "${window_horz_maxed}" ]; then
     72     wmctrl -ir "$window_id" -b add,maximized_horz
     73 elif [ -n  "${window_vert_maxed}" ]; then
     74     wmctrl -ir "$window_id" -b add,maximized_vert
     75 fi