How to Find and Grid Windows of a Specific Application – Bash Scripting

bashscriptswindow-managerwmctrl

I'm trying to write a script to identify all open chrome windows and move them into a grid layout on a large screen

I'm not sure how to find out what the best resolutions would be so I was going to manually add them in to a array so if 1 chrome window was available then maximise that, if 2 chrome windows available then go to a array for sizes for that?

At the moment I can move every window on the screen (does break my display when doing this) but I can see to work out how to move just the chrome screens?

The script below are some ideas I had but please point in the correct direction as at the moment the script doesn't work

#!/bin/bash
#Chrome window crontroller 


# Monitor 1920 X 1800

# Choose array for number of screens available 

# Different screen positions 
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;

ChromesAvailable()
{
    CA=$(wmctrl -lx | grep Chromium | wc -l)
}


GetNumOfChrome()
{
  WID=$(wmctrl -l | grep n | awk '{print$1}')
  #echo "INFO: window id = $WID"
}


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

        *)
            echo "ERROR: invalid option $1"
            echo "see --help for usage"
            exit 1
            ;;
  esac
  exit 0
else
for n in win{1..4}; do
    GetNumOfChrome
    PlaceWindow
done

fi

Edited – To explain things better 🙂

Using grep n will load every open window on the system so I tried to use grep Chromimum but the script doesn't like this

 GetNumOfChrome()
    {
      WID=$(wmctrl -l | grep n | awk '{print$1}')
      #echo "INFO: window id = $WID"
    }

Best Answer

A different approach is to arrange the windows form a pre- defined(customizable) grid (columns/rows)

An example:

enter image description here

rearranged into (cols set to 3, rows set to 2):

enter image description here

rearranged into (cols set to 4, rows set to 2):

enter image description here

The script below can be used to do that. As said, the number of columns&rows can be set, as well as the padding between the windows. The script calculates then the positions the windows should be arranged into, as well as their sizes.

Using the wmctrl command on Unity

The wmctrl command shows some peculiarities when used to move windows to- or very nearby the launcher or the panel. Therefore the margins:

left_margin = 70; top_margin = 30

cannot be set to zero. You have to keep at least a few px distance to both the panel and the launcher. I'd suggest leaving both margins- values as they are. All other values, padding, columns and rows you can play around with and set it as you like.

The script

#!/usr/bin/env python3
import subprocess
import getpass
import sys

#--- set your preferences below: columns, rows, padding between windows, margin(s)
cols = 2; rows = 2; padding = 10; left_margin = 70; top_margin = 30
#---

get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
def get_res():
    xr = get("xrandr").split(); pos = xr.index("current")
    return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]

# get resolution
res = get_res()
# define (calculate) the area to divide
area_h = res[0] - left_margin; area_v = res[1] - top_margin
# create a list of calculated coordinates
x_coords = [int(left_margin+area_h/cols*n) for n in range(cols)]
y_coords = [int(top_margin+area_v/rows*n) for n in range(rows)]
coords = sum([[(cx, cy) for cx in x_coords] for cy in y_coords], [])
# calculate the corresponding window size, given the padding, margins, columns and rows
w_size = [str(int(area_h/cols - padding)), str(int(area_v/rows - padding))]
# find windows of the application, identified by their pid
pids = [p.split()[0] for p in get("ps -e").splitlines() if sys.argv[1] in p]
w_list = sum([[w.split()[0] for w in get("wmctrl -lp").splitlines() if p in w] for p in pids], [])
print(pids, w_list, coords)
# remove possibly maximization, move the windows
for n, w in enumerate(w_list):
    data = (",").join([str(item) for item in coords[n]])+","+(",").join(w_size)
    cmd1 = "wmctrl -ir "+w+" -b remove,maximized_horz"
    cmd2 = "wmctrl -ir "+w+" -b remove,maximized_vert"
    cmd3 = "wmctrl -ir "+w+" -e 0,"+data
    for cmd in [cmd1, cmd2, cmd3]:
        subprocess.Popen(["/bin/bash", "-c", cmd])

How to use

  1. Make sure wmctrl is installed :)
  2. Copy thye script into an empty file, save it as rearrange_windows.py
  3. In the head section of the script, set your preferences
  4. Run it by the command:

    python3 /path/to/rearrange_windows.py <application>
    

    example: to rearrange chromium windows:

    python3 /path/to/rearrange_windows.py chromium
    

    to rearrange chrome windows

    python3 /path/to/rearrange_windows.py chrome
    

Note

The script can be used to put windows of any application into a grid, since the process name of the application is used as an argument.


EDIT

Dynamic version

below a dynamic version of the script, as requested in a comment. This version of the script calculates the number of columns and rows, depending on the number of windows. The proportions of the rearranged window(s) is similar to the proportions of the screen.

The setup and the use is pretty much the same as the version above, only the number of columns and rows is now set automatically.

#!/usr/bin/env python3
import subprocess
import getpass
import sys
import math

#--- set your preferences below: padding between windows, margin(s)
padding = 10; left_margin = 70; top_margin = 30
#---

get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
def get_res():
    xr = get("xrandr").split(); pos = xr.index("current")
    return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]

# find windows of the application, identified by their pid
pids = [p.split()[0] for p in get("ps -e").splitlines() if sys.argv[1] in p]
w_list = sum([[w.split()[0] for w in get("wmctrl -lp").splitlines() if p in w] for p in pids], [])
# calculate the columns/rows, depending on the number of windows
cols = math.ceil(math.sqrt(len(w_list))); rows = cols
# define (calculate) the area to divide
res = get_res()
area_h = res[0] - left_margin; area_v = res[1] - top_margin
# create a list of calculated coordinates
x_coords = [int(left_margin+area_h/cols*n) for n in range(cols)]
y_coords = [int(top_margin+area_v/rows*n) for n in range(rows)]
coords = sum([[(cx, cy) for cx in x_coords] for cy in y_coords], [])
# calculate the corresponding window size, given the padding, margins, columns and rows
if cols != 0:
    w_size = [str(int(area_h/cols - padding)), str(int(area_v/rows - padding))]
# remove possibly maximization, move the windows
for n, w in enumerate(w_list):
    data = (",").join([str(item) for item in coords[n]])+","+(",").join(w_size)
    cmd1 = "wmctrl -ir "+w+" -b remove,maximized_horz"
    cmd2 = "wmctrl -ir "+w+" -b remove,maximized_vert"
    cmd3 = "wmctrl -ir "+w+" -e 0,"+data
    for cmd in [cmd1, cmd2, cmd3]:
        subprocess.call(["/bin/bash", "-c", cmd])

See below the examples with a varying number of opened windows:

enter image description here enter image description here

enter image description here enter image description here

Explanation (second script)

Finding the specific windows

  1. The command:

    wmctrl -lp
    

    lists all windows, in the format:

    0x19c00085  0 14838  jacob-System-Product-Name *Niet-opgeslagen document 1 - gedit
    

    where the first column is the window's unique id, and the third column is the pid of the application that owns the window.

  2. The command:

    ps -e
    

    lists all processes, in the format:

    14838 ?        00:00:02 gedit
    

    where the first column is the application's pid, the last one is the process name.

  3. By comparing these two lists, we can find all windows (id of-) which belong to a specific application (called w_list in the script, as the result of line 17/18 in the script):

    pids = [p.split()[0] for p in get("ps -e").splitlines() if sys.argv[1] in p]
    w_list = sum([[w.split()[0] for w in get("wmctrl -lp").splitlines() if p in w] for p in pids], [])
    

Calculating the number of rows/columns

  1. If we make the windows of the same proportions as the screen, that means the number of columns is equal to the number of rows.
  2. That implies that both the number of colums and rows are equal to the rounded up square root of the number of windows to rearrange. That is done in line 20:

    cols = math.ceil(math.sqrt(len(w_list))); rows = cols
    

Calculating the window size and position

  1. Once we have the number of columns, all we need to do is divide the available area (screen resolution - left margin/top margin) in the columns/rows and we have the targeted window size, which is then diminished by the padding, as set in the head of the script:

    w_size = [str(int(area_h/cols - padding)), str(int(area_v/rows - padding))]
    
  2. The horizontal (x) positions are the result of the product(s) of the horizontal window size (including padding) times the column number, in a range of the number of columns. for example: if I have 3 colums of 300 px, the resulting x-positions are:

    [0, 300, 600]
    
  3. The vertical (y) positions are calculated likewise. Both lists are then combined into a list of coordinates, in which the windows will be rearranged.

    This is done in line 26-28 of the script:

    x_coords = [int(left_margin+area_h/cols*n) for n in range(cols)]
    y_coords = [int(top_margin+area_v/rows*n) for n in range(rows)]
    coords = sum([[(cx, cy) for cx in x_coords] for cy in y_coords], [])
    
  4. The actual rearranging finally (after unmaximizing possibly maximized windows) is done from line 33 and further.