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:
rearranged into (
cols
set to 3,rows
set to 2):rearranged into (
cols
set to 4,rows
set to 2):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: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
How to use
wmctrl
is installed :)rearrange_windows.py
Run it by the command:
example: to rearrange
chromium
windows:to rearrange
chrome
windowsNote
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.
See below the examples with a varying number of opened windows:
Explanation (second script)
Finding the specific windows
The command:
lists all windows, in the format:
where the first column is the window's unique id, and the third column is the pid of the application that owns the window.
The command:
lists all processes, in the format:
where the first column is the application's pid, the last one is the process name.
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):Calculating the number of rows/columns
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:
Calculating the window size and position
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: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:
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:
The actual rearranging finally (after unmaximizing possibly maximized windows) is done from line 33 and further.