/
activate-window
68 lines (63 loc) · 2.61 KB
/
activate-window
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# Attempt to activate a window whose title has the given prefix.
#
# This function works best when using Gnome Shell, with the extension
# https://extensions.gnome.org/extension/5021/activate-window-by-title/
# (Activate Window By Title) installed.
#
# If the extension is not available, we try two fallbacks:
# * using org.gnome.Shell.Eval (requires unsafe mode since Gnome 41)
# * using xdotool (only works in X sessions; prefix is case-insensitive)
# Please note that neither fallback has been tested very well.
function activate-window {
activate-window-gnome-shell-extension "$1" || activate-window-gnome-shell "$1" || activate-window-xdotool "$1"
}
function activate-window-gnome-shell-extension {
busctl --user call \
org.gnome.Shell \
/de/lucaswerkmeister/ActivateWindowByTitle \
de.lucaswerkmeister.ActivateWindowByTitle \
activateByPrefix \
s "$1" \
| (
IFS=' ' read -r b found
[[ $b == b ]] || { printf >&2 'dbus call de.lucaswerkmeister.ActivateWindowByTitle: expected return type b but was %q\n' "$b"; exit 1; }
[[ $found == true ]] || exit 1
)
}
function activate-window-gnome-shell {
local string="${1//'\'/'\\'}"
local string='"'"${string//'"'/'\"'}"'"'
busctl --user call org.gnome.Shell /org/gnome/Shell org.gnome.Shell Eval s '
(function() {
for ( const actor of global.get_window_actors() ) {
const window = actor.get_meta_window(),
title = window.get_title();
if ( title.startsWith( '"$string"' ) ) {
window.activate( global.get_current_time() );
return true;
}
}
return false;
})();
' | (
IFS=' ' read -r bs success result
[[ $bs == bs ]] || { printf >&2 'dbus call org.gnome.Shell.Eval: expected return type bs but was %q\n' "$bs"; exit 1; }
[[ $success == true ]] || { printf >&2 'dbus call org.gnome.Shell.Eval: expected success=true but was %q\nResult: %s\n' "$success" "$result"; exit 1; }
[[ $result == '"true"' ]] || exit 1
)
}
function activate-window-xdotool {
local pattern="${1//'\'/'\\'}"
pattern="${pattern//'.'/'\.'}"
pattern="${pattern//'['/'\['}"
pattern="${pattern//'('/'\('}"
pattern="${pattern//'^'/'\^'}"
pattern="${pattern//'$'/'\$'}"
pattern="${pattern//'*'/'\*'}"
pattern="${pattern//'+'/'\+'}"
pattern="${pattern//'?'/'\?'}"
pattern="${pattern//'{'/'\{'}"
pattern="${pattern//'|'/'\|'}"
xdotool search --name "^$pattern" windowactivate &>/dev/null
}