-
Notifications
You must be signed in to change notification settings - Fork 1
/
shortcut-window.sh
executable file
·158 lines (130 loc) · 5.03 KB
/
shortcut-window.sh
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# This tool allows a user to create a shortcut to quickly activate a certain
# window in X11. Window key shortcuts are registered globally, therefore, one
# will also jump to a given window if it is on a different virtual desktop.
# Required software: xdotool
# Commands
# shortcut-window.sh store name
#
# Store the currently active window under the given `name`.
# shortcut-window.sh activate name
#
# Activates the window stored under the given name. Does nothing if the window
# cannot be found)
config_file="$HOME/.config/shortcut-win.data"
if [ ! -e "$config_file" ] ; then
touch "$config_file"
fi
function clear_name() {
tr -dc '[a-zA-Z0-9]' <<< "$1"
}
function find_window_name() {
name=$( clear_name "$1" )
winname=$( awk '{ print $3 }' <<< "$line" | tr -d -c '[a-zA-Z0-9_.*+]' )
echo $winname
}
function find_window() {
name=$( clear_name "$1" )
if ! line=$( grep "^$name " $config_file ) ; then
notify-send --urgency=low -i error "No window stored using name '$name'"
exit 1
fi
winid=$( awk '{ print $2 }' <<< "$line" )
if ! xdotool getwindowname $winid > /dev/null ; then
windowname=$( find_window_name "$2" )
winregex="$( sed 's/_/ /g' <<< $windowname )"
winid=""
[ ! -z "$windowname" ] && winid=$(xdotool search --name "^$winregex$" | head -n1 )
if [ -z "$winid" ] ; then
notify-send --urgency=low -i error "No window stored or found using name '$name'"
exit 1
fi
fi
echo $winid
}
if [ "$1" == "help" ] || [ "$1" == "--help" ]; then
cat <<EOF
shortcut-window.sh command [command-args]
Utility that allows to assign keys to open X-windows and send keyboard
shortcuts to keyed windows. It is particular useful when used in with
custom shortcuts in a window manager. This tool then can be used to assign
keyboard shortcuts to windows like in a RTS game.
Commands:
store key Store the currently active window under the key
`key`. Example: "shortcut-window.sh store window-1"
activate key Activate the window that is stored udner the
given `key` name. Example:
"shortcut-window.sh activate window-1"
rec_key key keyid Store a key combination that can be sent to a window
that as an assigned key `key` refers to the name of
a key assigned to a window using the "store" command.
`keyid` is an identifier that is used to store the
key under. A popup will be shown that allows a user
to enter a key combination that sahll be sent to the
window that `key` refers to. The combination of
window+shortcut will be stored under the key `keyid`.
send_key keyid Send the previously stored key combination to the
stored window.
EOF
exit 0
fi
if [ "$1" == "store" ] ; then
if [ -z "$2" ] ; then
notify-send --urgency=low -i error "No name for window specified."
exit 1
fi
if ! winid=$(xdotool getactivewindow) ; then
notify-send --urgency=low -i error "No active window found."
exit 1
fi
winname=$(xdotool getwindowname $winid | sed -E 's/[^a-zA-Z0-9 ]/./g;s/ /_/g;s/\.+/.+/g')
name=$( clear_name "$2" )
grep -v "^$name " $config_file > $config_file.2
mv $config_file.2 $config_file
echo "$name $winid $winname" >> $config_file
notify-send --urgency=low -i info "Assigned '$name' => '`xdotool getwindowname $winid`'"
exit 0
fi
if [ "$1" == "activate" ] ; then
if ! winid=$(find_window "$2") ; then
exit 1
fi
xdotool windowactivate $winid
exit 0
fi
if [ "$1" == "rec_key" ] ; then
win_key_name=$( clear_name "$2" )
if ! winid=$(find_window "$win_key_name") ; then
exit 1
fi
window_name="$(xdotool getwindowname $winid)"
name=$( clear_name "$3" )
if [ -z "$name" ] ; then
notify-send --urgency=low -i error "No keyid provided for rec_key command (3rd argument)"
exit 1
fi
if ! key="$(zenity --entry --text='Key combination to send to window on '"'$win_key_name'"' for keyid '"'$name'")" ; then
exit 0
fi
key=$( tr -d '[:blank:]' <<< "$key" )
grep -v "^!rec_key $name " $config_file > $config_file.2
mv $config_file.2 $config_file
echo "!rec_key $name $win_key_name $key" >> $config_file
notify-send --urgency=low -i info "Registered sending key(s) '$key' to window '$window_name' under keyid '$name'"
exit 0
fi
if [ "$1" == "send_key" ] ; then
name=$( clear_name "$2" )
if ! line=$(grep "\\!rec_key $name " $config_file) ; then
notify-send --urgency=low -i error "No key bound for keyid '$name'"
exit 1
fi
read _ _ win_key_name key <<< "$line"
if ! winid=$( find_window "$win_key_name" ) ; then
exit 1
fi
xdotool key --clearmodifiers --delay 100 --window $winid $( sed 's/,/ /g' <<< "$key" )
exit 0
fi
echo "Invalid argument: $1 (use --help to show help)"
exit 1