Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Moving windows programmatically in Gnome under Wayland (or X)
#!/bin/bash
# -------- THIS SCRIPT IS OUTDATED -------
# Gnome has removed the Shell.Eval endpoint, so it won't work anymore.
# Feel free to use it as inspiration for something else.
# Original comment below.
# ----------------------------------------
# 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"
@marvinthepa
Copy link

marvinthepa commented Jun 14, 2022

https://developer.gnome.org/meta/stable/MetaWindow.html

This leads to a 404.
Try https://gjs-docs.gnome.org/meta9~9_api/ (or https://gjs-docs.gnome.org/ and searching for Meta, when that one breaks as well).
I also found https://gjs-docs.gnome.org/shell01~0.1_api/ helpful.

@MortenStabenau
Copy link
Author

MortenStabenau commented Jun 14, 2022

Actually, this gist is outdated. Newer versions of Gnome have removed the Shell.Eval method so this exact method won't work anymore. One would have to write a full Gnome Extension, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment