-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-command
executable file
·238 lines (226 loc) · 6.19 KB
/
type-command
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env bash
# When called with --key the CorC modifier maps to ctrl in Linux and to Cmd in Mac OS
UNAME=$(uname)
if [[ "${UNAME}" == "Darwin" ]]
then
type -a hs &>/dev/null
if [ $? -ne 0 ]
then
send-notify -a "Failure" "Hammerspoon CLI is not installed"
exit 1
fi
else
type -a xdotool &>/dev/null
if [ $? -ne 0 ]
then
send-notify -a "Failure" "xdotool is not installed"
exit 1
fi
fi
function translate_linux_key_to_mac() {
case "$1" in
CorC) echo cmd ;; # Artificial ControlOrCommand key
CorC_R) echo rightcmd ;; # Artificial ControlOrCommand key
ctrl) echo ctrl ;;
Control_L) echo ctrl ;;
Control_R) echo rightctrl ;;
shift) echo shift ;;
Shift_L) echo shift ;;
Shift_R) echo rightshift ;;
alt) echo alt ;;
Alt_L) echo alt ;;
Alt_R) echo rightalt ;;
Backspace) echo delete ;;
Tab) echo tab ;;
Return) echo enter ;;
Escape) echo escape ;;
Delete) echo forwarddelete ;;
Home) echo home ;;
Left) echo left ;;
Up) echo up ;;
Right) echo right ;;
Down) echo down ;;
Page_Up) echo page-up ;;
Page_Down) echo page-down ;;
End) echo end ;;
KP_Enter) echo padenter ;;
KP_Home) echo home ;;
KP_Left) echo left ;;
KP_Up) echo up ;;
KP_Right) echo right ;;
KP_Down) echo down ;;
KP_Page_Up) echo pageup ;;
KP_Page_Down) echo pagedown ;;
KP_End) echo end ;;
KP_Delete) echo padclear ;;
KP_Equal) echo pad= ;;
KP_Multiply) echo pad* ;;
KP_Add) echo pad+ ;;
KP_Subtract) echo pad- ;;
KP_Decimal) echo . ;;
KP_Divide) echo pad/ ;;
KP_0) echo pad0 ;;
KP_1) echo pad1 ;;
KP_2) echo pad2 ;;
KP_3) echo pad3 ;;
KP_4) echo pad4 ;;
KP_5) echo pad5 ;;
KP_6) echo pad6 ;;
KP_7) echo pad7 ;;
KP_8) echo pad8 ;;
KP_9) echo pad9 ;;
F1) echo f1 ;;
F2) echo f2 ;;
F3) echo f3 ;;
F4) echo f4 ;;
F5) echo f5 ;;
F6) echo f6 ;;
F7) echo f7 ;;
F8) echo f8 ;;
F9) echo f9 ;;
F10) echo f10 ;;
F11) echo f11 ;;
F12) echo f12 ;;
F13) echo f13 ;;
F14) echo f14 ;;
F15) echo f15 ;;
F16) echo f16 ;;
F17) echo f17 ;;
F18) echo f18 ;;
F19) echo f19 ;;
F20) echo f20 ;;
space | " ") echo space ;;
exclam | \!) echo 1 ; return 1 ;;
quotedbl | \") echo \' ; return 1 ;;
numbersign | \# ) echo 3 ; return 1 ;;
dollar | \$) echo 4 ; return 1 ;;
percent | \%) echo 5 ; return 1 ;;
ampersand | \&) echo 6 ; return 1 ;;
apostrophe) echo \' ;;
parenleft | \() echo 9 ; return 1 ;;
parenright | \)) echo 0 ; return 1 ;;
asterisk | \*) echo 8 ; return 1 ;;
plus | \+) echo = ; return 1 ;;
comma) echo , ;;
minus) echo - ;;
period) echo . ;;
slash) echo \/ ;;
colon | \:) echo \; ; return 1 ;;
semicolon) echo \; ;;
less | \<) echo , ; return 1 ;;
equal) echo \= ;;
greater | \>) echo . ; return 1 ;;
question | \?) echo / ; return 1 ;;
at | \@) echo 2 ; return 1 ;;
bracketleft) echo \[ ;;
backslash | \\) echo \\\\ ;;
bracketright) echo \] ;;
asciicircum | \^) echo 6 ; return 1 ;;
underscore| \_) echo - ; return 1 ;;
grave | \`) echo \` ;;
braceleft) echo \[ ; return 1 ;;
bar | \|) echo \\\\ ; return 1 ;;
braceright) echo \] ; return 1 ;;
asciitilde | \~) echo \` ; return 1 ;;
*) echo $1 ;;
esac
return 0
}
function append_modifier() {
if [[ -n "$1" ]]
then
echo "$1, \"$2\""
else
echo "\"$2\""
fi
}
function build_hs_script() {
FILENAME=$1
shift
echo "#!/usr/local/bin/hs" >"${FILENAME}"
for KEY in "${@}" ; do
unset MAC_KEYS
NEEDS_SHIFT=0
IFS='+' ; read -r -a KEY_PARTS <<< "${KEY}"
let last=${#KEY_PARTS[@]}-1
for (( i = 0 ; i < ${#KEY_PARTS[@]} ; i++ ))
do
MAC_KEYS[$i]=$(translate_linux_key_to_mac "${KEY_PARTS[$i]}") || NEEDS_SHIFT=1
done
MODIFIERS=''
for (( i = 0 ; i < ${#MAC_KEYS[@]}-1; i++ ))
do
MODIFIERS=$(append_modifier "${MODIFIERS}" "${MAC_KEYS[$i]}")
done
if [[ -z ${MODIFIERS} && ${NEEDS_SHIFT} == 1 ]]
then
MODIFIERS='"shift"'
fi
echo "hs.eventtap.keyStroke({${MODIFIERS}}, \"${MAC_KEYS[${last}]}\")" >>"${FILENAME}"
done
echo "return 0"
# cat "${FILENAME}"
}
function append() {
if [[ -n "$1" ]]
then
echo "$1 $2"
else
echo "$2"
fi
}
if [[ $1 == "--key" ]]
then
COMMAND=key
shift
else
COMMAND=type
fi
NL=true
if [[ "$1" == "-n" ]]
then
unset NL
shift
fi
(
if [[ "${UNAME}" == "Darwin" ]]
then
if [[ ${COMMAND} == type ]]
then
hs <<<"hs.eventtap.keyStrokes(\"${*}\")"
if [[ ${NL} == "true" ]]
then
hs <<<"hs.eventtap.keyStroke({}, \"return\")"
fi
else
source tmp-dir # TODO Add path to the tmp-dir
gettmpfilename
# LOCAL_TMPFILE=${HOME}/log/hs.tmp
build_hs_script "${LOCAL_TMPFILE}" "${@}"
RC=$(hs -q "${LOCAL_TMPFILE}") || send-notify -s "Failure" "Hammerspoon failed to submit keystrokes"
exit ${RC}
fi
else
if [[ ${COMMAND} == type ]]
then
xdotool sleep 0.5 type --clearmodifiers ${*}
if [[ ${NL} == "true" ]]
then
xdotool sleep 0.25 key Return
fi
else
NEW_POS=''
for i
do
if [[ "$i" =~ CorC\+ ]]
then
NEW_POS=$(append "${NEW_POS}" "${i/CorC\+/ctrl+}")
else
NEW_POS=$(append "${NEW_POS}" "${i/CorC_R\+/Control_R+}")
fi
done
set -- ${NEW_POS}
xdotool sleep 0.5 key ${*} || send-notify -s "Failure" "xdotool failed to submit keystrokes"
fi
fi
) &>/dev/null & disown