Scripting

Toggle Window Between Monitors

This script will take the current foreground app and move it to your second monitor (and vice versa)

I took it from (page here when I can find it again...) but that script you had to enter the screen resolutions in the file. As I have different monitors at home and work, I wanted this to autodetect, so modified it.

Map a key combo to it and you should be good to go.


#!/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.
#++++++++++++++++

# window title bar height (default title bar height in Gnome)
h_tbar=29

function check_progs {
#Check existence of certain required programs
  PROGS="xdotool xwininfo xrandr"
  for name in $PROGS; do
    if [ ! `which $name` ];then
      echo -e "*Program “$name” is not installed or not in PATH."
      exit 1
    fi
  done
}

check_progs
set -e

function get_number_of_displays {
  DISPLAY_COUNT=$(xrandr | grep " connected" | wc -l)
}

function grab_left_display {
  local DISP=$(xrandr |grep " connected" |grep "+0+0" | grep -Po "[\d]*x[\d]*\+0\+0" | cut -d'+' -f1)
  echo "$DISP"
}

function grab_right_display {
  local DISP=$(xrandr |grep " connected" |grep -v "+0+0" | grep -Po "[\d]*x[\d]*\+[\d]+\+[\d]+" | cut -d'+' -f1)
  echo "$DISP"
}

function get_horiz {
  local RES=$(echo $1 | cut -d'x' -f1 )
  echo "$RES"
}

function get_vert {
  local RES=$(echo $1 | cut -d'x' -f2 )
  echo "$RES"
}

LEFT=$(grab_left_display)
RIGHT=$(grab_right_display)

# resolution of left monitor
w_r_monitor=$(get_horiz $RIGHT)
h_r_monitor=$(get_vert $RIGHT)
# resolution of right monitor
w_l_monitor=$(get_horiz $LEFT)
h_l_monitor=$(get_vert $LEFT)

# focus on active window
window=`xdotool getactivewindow`

# 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}'`

# window on left monitor
if [ "$x" -lt "$w_l_monitor" ]; then
  new_x=$(($x+$w_l_monitor))
  new_y=$(($y-$h_tbar))
  xdotool windowmove $window $new_x $new_y
  # retain maximization
  if [ "$w" -eq "$w_l_monitor" ]; then
    xdotool windowsize $window 100% 100%
  # adjust height
  elif [ "$h" -gt $(($h_r_monitor-$h_tbar)) ]; then
    xdotool windowsize $window $w $(($h_r_monitor-$h_tbar))
  fi

# window on right monitor
else
  new_x=$(($x-$w_l_monitor))
  new_y=$(($y-$h_tbar))
  xdotool windowmove $window $new_x $new_y
  # retain maximization
  if [ "$w" -eq "$w_r_monitor" ]; then
      xdotool windowsize $window 100% 100%
  # adjust height
  elif [ "$h" -gt $(($h_l_monitor-$h_tbar)) ]; then
    xdotool windowsize $window $w $(($h_l_monitor-$h_tbar))
  fi
fi