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 grid 3 x 3 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, it is displayed a script that achieves partially what I want to do. The script below, which was based from 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

  • Thanks for the feedback (apologies for the late reply). Moved the way I did it below. Do not know about the touch command, as I mentioned in the post, I retrieved that code from another post. Anyway, I made reference to your recommendations in the post below about the dangers of using that approach. – André Lourenço Oct 26 '22 at 18:56

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
  • 88,010
  • Thank you for the reply. I have tested the script now on Ubuntu 20 and it is doing something but not exactly what I want. I want to be able to click on any window and click on a key (let's say 1) to adjust and resize it accordingly to the x/y coordinates supplied in the script. Currently, only the terminal window is adjusted. Apologies, I should have been clearer. I have edited the original post accordingly. – André Lourenço Oct 03 '22 at 12:42
  • So I put the script in the location you suggested, log out of the session for the functionality to be added to PATH and tried to run different variations, like sudo tilling 1 or sudo tilling.sh 1. It gave me the error that the command was not found. If I just run the script directly from the terminal, it will just resize the terminal window based on the provided key and it won't work with other windows when I click to activate them. – André Lourenço Oct 04 '22 at 12:46
  • Thanks. Your comment helped me find a solution, which is not as elegant. I have posted my solution in the original post. – André Lourenço Oct 07 '22 at 10:33
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.