Skip to content

Instantly share code, notes, and snippets.

@EKami
Last active October 19, 2022 07:18
Show Gist options
  • Save EKami/d663a9ee56e782ad8741dab36e10efa0 to your computer and use it in GitHub Desktop.
Save EKami/d663a9ee56e782ad8741dab36e10efa0 to your computer and use it in GitHub Desktop.
Open and close terminator on Gnome 3 wayland
#!/bin/bash
# https://superuser.com/questions/142945/bash-command-to-focus-a-specific-window/1627429#1627429
# To use, set a shortcut in Gnome like this: bash -c '/home/user/launch_focus_min_wayland.sh terminator'
# To check the available function names, do ALT + F2 and type "lg"
app=$1
if [[ $app == terminator ]]; then
process_name=/usr/bin/terminator
else
process_name=$app
fi
#pid=$(pidof $process_name) # pidof didn't work for terminator
pid=$(pgrep -f $process_name)
shell_name='/bin/bash' # Since it's bash that started it
# If it isn't launched, then launch
if [ -z $pid ]; then
$app
else
# If it is launched then check if it is focused
foc=$(gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /org/gnome/Shell \
--method org.gnome.Shell.Eval "
global
.get_window_actors()
.map(a=>a.meta_window)
.find(w=>w.has_focus())
.get_wm_class()" \
| cut -d'"' -f 2)
if [[ $foc == 'terminator' ]]; then
# if it is focused, then minimize
gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /org/gnome/Shell \
--method org.gnome.Shell.Eval \
"var mw = global.get_window_actors().map(w=>w.meta_window).find(mw=>mw.get_title().includes('$shell_name'));
mw.minimize()"
else
# if it isn't focused then get focus
gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /org/gnome/Shell \
--method org.gnome.Shell.Eval \
"var mw = global.get_window_actors().map(w=>w.meta_window).find(mw=>mw.get_title().includes('$shell_name'));
mw.activate(0)"
fi
fi
exit 0
@jplewa
Copy link

jplewa commented Nov 7, 2021

For me, .find(mw=>mw.get_title().includes('$shell_name')) didn't work. Maybe because I use zsh instead of bash? Not sure. Regardless - I replaced it with .find(mw=>mw.get_wm_class().includes('terminator')) and it works exactly as I hoped. Great script, thank you so much!

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