Ubuntu – Arrange windows by script

displaymultiple-monitorsxfce

I'm using my laptop in different configurations.
Sometimes i'm using only laptop, with LVDS1 output. Sometimes i've one monitor(DP1) on right of LVDS. Sometimes on left. Sometimes i have two monitors(DP2/DP3) – one on left, and one on right of LVDS

I would like to setup my desktop for this environment. I want xfce panel always on LVDS, web browser – always on external monitor(and if are 2 monitors – on right), communicator(pidgin) on left (DP2) or LVDS if there is no left monitor.

I've prepared set of script to configurue xserver using xrandr – for all configurations. position of panel i'm setting by xfconf-query.

But – how to move specific application to specific monitor?
For example – how to run all pidgin applications to DP2? I don't want do it by mouse – i want to move all filtered xwindow clients to specific display. It's possible?

Best Answer

If your main objective is to avoid using the mouse, this script allows for a keyboard shortcut to move the windows between monitors. It works great for me on my triple monitor setup. Then you can assign a keyboard shortcut to tile after moving to a new monitor.

  1. Download the script below. Save under ~/bin/
  2. Make it executable: chmod +x ~/bin/move-to-next-monitor

Source: https://github.com/jc00ke/bin/blob/master/move-to-next-monitor. Thanks to jc00ke!

#!/bin/sh
#
# Move the current window to the next monitor.
#
# Also works only on one X screen (which is the most common case).
#
# Props to
# http://icyrock.com/blog/2012/05/xubuntu-moving-windows-between-monitors/
#
# Unfortunately, both "xdotool getwindowgeometry --shell $window_id" and
# checking "-geometry" of "xwininfo -id $window_id" are not sufficient, as
# the first command does not respect panel/decoration offsets and the second
# will sometimes give a "-0-0" geometry. This is why we resort to "xwininfo".
# Make sure your largest monitor is set to Primary display.

screen_width=`xdpyinfo | awk '/dimensions:/ { print $2; exit }' | cut -d"x" -f1`
screen_height=`xdpyinfo | awk '/dimensions:/ { print $2; exit }' | cut -d"x" -f2`
display_width=`xdotool getdisplaygeometry | cut -d" " -f1`
display_height=`xdotool getdisplaygeometry | cut -d" " -f2`
window_id=`xdotool getactivewindow`

# Remember if it was maximized.
window_state=`xprop -id $window_id _NET_WM_STATE | awk '{ print $3 }'`

# Un-maximize current window so that we can move it
wmctrl -ir $window_id -b remove,maximized_vert,maximized_horz

# Read window position
x=`xwininfo -id $window_id | awk '/Absolute upper-left X:/ { print $4 }'`
y=`xwininfo -id $window_id | awk '/Absolute upper-left Y:/ { print $4 }'`

# Subtract any offsets caused by panels or window decorations
x_offset=`xwininfo -id $window_id | awk '/Relative upper-left X:/ { print $4 }'`
y_offset=`xwininfo -id $window_id | awk '/Relative upper-left Y:/ { print $4 }'`
x=`expr $x - $x_offset`
y=`expr $y - $y_offset`

# Compute new X position
new_x=`expr $x + $display_width`
# Compute new Y position
new_y=`expr $y + $display_height`

# If we would move off the right-most monitor, we set it to the left one.
# We also respect the window's width here: moving a window off more than half its width won't happen.
width=`xdotool getwindowgeometry $window_id | awk '/Geometry:/ { print $2 }'|cut -d"x" -f1`
if [ `expr $new_x + $width / 2` -gt $screen_width ]; then
  new_x=`expr $new_x - $screen_width`
fi

height=`xdotool getwindowgeometry $window_id | awk '/Geometry:/ { print $2 }'|cut -d"x" -f2`
if [ `expr $new_y + $height / 2` -gt $screen_height ]; then
  new_y=`expr $new_y - $screen_height`
fi

# Don't move off the left side.
if [ $new_x -lt 0 ]; then
  new_x=0
fi

# Don't move off the bottom
if [ $new_y -lt 0 ]; then
  new_y=0
fi

# Move the window
xdotool windowmove $window_id $new_x $new_y

# Maintain if window was maximized or not
if [ "${window_state}" = "_NET_WM_STATE_MAXIMIZED_HORZ," ]; then
    wmctrl -ir $window_id -b add,maximized_vert,maximized_horz
fi
  1. If not already installed, install xdotool and wmctrl: sudo apt-get install xdotool wmctrl
  2. Next assign a keyboard shortcut for the script under Keyboard > Application Shortcuts. (I use Alt+Right. Then to tile right I use Ctrl+Alt+Right and to tile left Ctrl+Alt+Left. Window tiling keyboard shortcuts are under Window Manager.)
    Source: http://makandracards.com/makandra/12447-how-to-move-a-window-to-the-next-monitor-on-xfce-xubuntu

For different size monitors
I have a 1920x1080 center monitor and two 1440x900 side monitors.

display_width=`xdotool getdisplaygeometry | cut -d" " -f1`
display_height=`xdotool getdisplaygeometry | cut -d" " -f2`

The above two commands output the dimensions of my largest monitor even if the window happens to be on a side monitor. Therefore the script always moved the window according to these dimensions. This caused the script to not always move to the next monitor, but sometimes it would move to the previous monitor. I took my overall screen width (4800) and divided it by three. Now it always moves to the next monitor.

display_width="1600"
display_height=`xdotool getdisplaygeometry | cut -d" " -f2`
Related Question