Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created September 28, 2021 13:42
Show Gist options
  • Save unixpickle/eb0823bde4b1e279d84c38094df11671 to your computer and use it in GitHub Desktop.
Save unixpickle/eb0823bde4b1e279d84c38094df11671 to your computer and use it in GitHub Desktop.
Center window in GNOME
#!/bin/bash
#
# Center a GNOME window, given its title. All windows with the given title are
# moved to the center of their respective displays.
#
# Example:
#
# bash center_window.sh "foo.png - image editor"
#
run_javascript() {
gdbus call -e -d org.gnome.Shell -o /org/gnome/Shell -m org.gnome.Shell.Eval "$1"
}
target_title="$1"
script=$(cat <<EOF
(function(target_title) {
let num_moved = 0;
const actors = global.get_window_actors();
for (let i = 0; i < actors.length; i++) {
const window = actors[i].get_meta_window();
if (window.get_title() == target_title) {
const display = window.get_display();
const [dw, dh] = display.get_size();
const frame = window.get_frame_rect();
const x = (dw - frame.width) / 2;
const y = (dh - frame.height) / 2;
window.move_frame(0, x, y);
num_moved += 1;
}
}
return "moved " + num_moved + " windows.";
})
EOF
)
run_javascript "$script"\("\"$target_title\""\)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment