Programatically open windows and arrange in grid pattern on screen

Needed a way to tile web browser windows for a system monitoring thing I'm working on. Came across this great tool called wmctrl that allows programmatic control of application windows. Wrote a script to automate launching a bunch of separate Chromium web browser windows, then resize them and arrange in a grid. See also this post on launching multiple Chromium browser windows from the command line.

Here's a video of the script in action:

It's specifically for my dual 1920x1080 monitor setup, but should be easy to adapt to other configurations.

Here's the script:

#!/bin/bash

# Opens 11 Chromium browser windows and arranges in a grid

# window sizes and placement coordinates
G=0
win1_X=5;      win1_Y=24;    win1_W=639;   win1_H=499;
win2_X=642;    win2_Y=24;    win2_W=639;   win2_H=499;
win3_X=1280;   win3_Y=24;    win3_W=639;   win3_H=499;
win4_X=5;      win4_Y=552;   win4_W=639;   win4_H=499;
win5_X=642;    win5_Y=552;   win5_W=639;   win5_H=499;
win6_X=1280;   win6_Y=552;   win6_W=639;   win6_H=499;
win7_X=1920;   win7_Y=24;    win7_W=639;   win7_H=499;
win8_X=2562;   win8_Y=24;    win8_W=1277;  win8_H=499;
win9_X=1921;   win9_Y=552;   win9_W=639;   win9_H=499;
win10_X=2561;  win10_Y=552;  win10_W=639;  win10_H=499;
win11_X=3200;  win11_Y=552;  win11_W=639;  win11_H=499;

Usage() 
{
cat </dev/null &
}

KillAndCleanup()
{
  pkill -f /tmp/chromium-tmp-
  sleep 1
  rm -rf /tmp/chromium-tmp-*
}

PlaceWindow()
{
  X=${n}_X; Y=${n}_Y; W=${n}_W; H=${n}_H; 
  wmctrl -i -r "$WID" -e $G,${!X},${!Y},${!W},${!H}
}

if [ "$#" -gt 0 ]; then
  case "$1" in
        -c|--cleanup)
            KillAndCleanup
            exit 0
            ;;
        -h|--help)
            Usage
            exit 0
            ;;
        *)
            echo "ERROR: invalid option $1"
            echo "see --help for usage"
            exit 1
            ;;
  esac
  exit 0
else
  for n in win{1..11}; do
    GetTempDir
    LaunchBrowser
    sleep 1
    GetWindowID
    PlaceWindow
  done
fi

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).