Skip to content

Instantly share code, notes, and snippets.

@soal
Forked from MortenStabenau/widescreen-resize.sh
Created March 3, 2021 15:02
Show Gist options
  • Save soal/4a49ac7aeeb8d0b54b8f5a6e9332e2b6 to your computer and use it in GitHub Desktop.
Save soal/4a49ac7aeeb8d0b54b8f5a6e9332e2b6 to your computer and use it in GitHub Desktop.
Moving windows programmatically in Gnome under Wayland (or X)
#!/bin/bash
# This script programmatically moves around the windows on my screens.
# The JavaScript can be prototyped in Looking Glass (ALT+F2 lg). The documentation of MetaWindow can be found here:
# https://developer.gnome.org/meta/stable/MetaWindow.html
set -e
if [ -z "$1" ]; then
echo "Please give a configuration (1-5)."
exit 1;
fi
case $1 in
1)
# Main screen left
X=0
Y=0
W=1146
H=1410
M=2 # Maximization mode, 1 -> horizontal, 2 -> vertical, 3 -> both
;;
2)
# Main screen middle
X=1146
Y=0
W=1148 # Middle one is a few pixels wider
H=1410
M=2
;;
3)
# Main screen right
X=2294
Y=0
W=1146
H=1410
M=2
;;
4) # Secondary screen (fullscreen)
X=3440
Y=0
W=1080
H=1920
M=3
;;
5)
# Main screen middle - wider (60%)
X=688
Y=0
W=2064
H=1410
M=2
;;
*)
echo "Invalid configuration $1."
exit 1
esac
JS="""
const GLib = imports.gi.GLib;
global.get_window_actors().forEach(function (w) {
var mw = w.meta_window;
if (mw.has_focus()) {
if (mw.get_maximized()) {
mw.unmaximize(3); // 3 is META_MAXIMIZE_BOTH, so unmaximizing the window in both directions
}
mw.move_resize_frame(0, $X, $Y, $W, $H);
if ($M) {
// Maximization needs to be delayed a little, so that the window has time to move
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 500, function() { mw.maximize($M); });
}
}
});
"""
gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval "$JS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment