0

What I want to do

I want to be able to click on a window (terminal window, any software window, etc.) to activate it and click on a shortcut key which will resize and move the active window to a specified size and xy coordinates on the screen. I will have several windows open (about 20) and I want to split them in a 3 x 3 grid per working environment very easily instead of adjusting them manually.

What I have tried so far

I have searched online and it appears the package xdotool provides the functionality to do that. I am trying to assign keys 1 to 9 (because I want a 3 x 3 grid) to adjust automatically each selected window to the respective position in a given working environment. Below is a script that achieves partially what I want to do. The script below, which was based on this here, only adjusts the terminal window (because the script is run there) when an argument is supplied.

How can I change this script so that it will run continuously so that when I select any window, I can press a key from 1 to 9 and the active window will be automatically resized and moved?

#!/bin/bash

grabbing screen width and height

SIZE=xdpyinfo | awk '/dimensions/{print $2}' SW=echo $SIZE | cut -f1 -dx SH=echo $SIZE | cut -f2 -dx

function that changes a window's position and size

function set_win { # args: pos_x, pos_y size_x, size_y # don't move Desktop if [ "$(xdotool getactivewindow getwindowname)" != "Desktop" ]; then xdotool getactivewindow windowsize $3 $4 xdotool getactivewindow windowmove $1 $2 fi }

case "$1" in 1) set_win 0 0 $((SW/3)) $((SH/3)) ;; 2) set_win $((SW/3)) 0 $((SW/3)) $((SH/3)) ;; 3) set_win $((2*SW/3)) 0 $((SW/3)) $((SH/3)) ;;

4) set_win 0 $((SH/3)) $((SW/3)) $((SH/3)) ;;
5) set_win $((SW/3)) $((SH/3)) $((SW/3)) $((SH/3)) ;;
6) set_win $((2*SW/3)) $((SH/3)) $((SW/3)) $((SH/3)) ;;

7) set_win 0 $((2*SH/3)) $((SW/3)) $((SH/3)) ;;
8) set_win $((SW/3)) $((2*SH/3)) $((SW/3)) $((SH/3)) ;;
9) set_win $((2*SW/3)) $((2*SH/3)) $((SW/3)) $((SH/3)) ;;

esac

peejay
  • 139

2 Answers2

2

That script will work as is, provided you are running on Xorg. If you use Ubuntu 22.04, you are running Wayland. Prior versions default to Xorg. If you wish, you can easily switch back to Xorg. If you do not wish to run Xorg instead of Wayland, the script will not work.

Once you are on Xorg, the script should work as is. However, note that the script requires an argument. That argument, a number between 1 and 9, tells the script which tile to create. See the case loop. In case "$1" in, "$1" is a placeholder for the first argument you provide to the script. Thus, for example,

tile_window 3

will resize the window and place it in the first row on the rightmost column of the 9x9 grid. This assumes that your script is called tile_window, and that it is placed in a directory that is in your search PATH, the list of directories that the system searches to find executables. ~/.local/bin is an excellent location, although you also can use ~/bin. Create the directory: it may not yet exist on your system. Then log out and back in - it will automatically be included in the search PATH.

The commands to assign the shortcut keys will be

tile_window 1
tile_window 2

...etc, which you wish to assign to 1 through 9.

You can also specify the full path to the executable as

/home/alourenco/.local/share/tile_window 1

assuming alourenco is your login name. The shortcut notation ~ for /home/alourenco, that works in the terminal, will not work in a command you assign to a shortcut key, so you need to write the path in full.

vanadium
  • 97,564
0

I ended up solving my issue using the approach below, although please see the comments posted by the user @vanadium about the dangers of doing this.

With the help of the user @vanadium and this post, I have managed to find a solution, which is not very elegant but it does what I want. I have created 9 scripts (one for each key) that look like this (example below shows key 1 binding which resizes and moves the active window to the upper left corner of the screen):

#!/bin/bash

grabbing screen width and height

SIZE=xdpyinfo | awk '/dimensions/{print $2}' SW=echo $SIZE | cut -f1 -dx SH=echo $SIZE | cut -f2 -dx

function that changes a window's position and size

function set_win { # args: pos_x, pos_y size_x, size_y # don't move Desktop if [ "$(xdotool getactivewindow getwindowname)" != "Desktop" ]; then xdotool getactivewindow windowsize $3 $4 xdotool getactivewindow windowmove $1 $2 fi }

set_win 0 0 $((SW/3)) $((SH/3))

Then I copied all scripts to /bin/, changed the working directory to that folder and run the following commands for each script (again, example of script, here named tilling_1.sh, with key binding for key 1):

sudo touch tilling_1.sh
sudo chmod +x tilling_1.sh

Then, I went to Ubuntu's keyboard menu, create a custom shortcut key and give a name of the command, put the name of the script in the "Command" field and assign the respective key.