-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendKeysOnlyInApp.lua
197 lines (158 loc) · 5.75 KB
/
SendKeysOnlyInApp.lua
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
-- https://github.com/Hammerspoon/hammerspoon/blob/master/SPOONS.md#hotkeys
-- https://stackoverflow.com/questions/63795560/how-can-i-prevent-hammerspoon-hotkeys-from-overriding-hotkeys-in-other-applicati
local logger = hs.logger.new("SendKeysOnlyInApp")
-- Helper Function
local function listToSet (list)
local set = {}
for _, l in ipairs(list) do
set[l] = true
end
return set
end
local function toTable(st)
return type(st) == 'table' and st or { st }
end
local function normalizeArgs(st)
if isTable(st) and isTable(st[1]) then
return st[1]
else
return st
end
end
--- app condition curry functions
local function conditionTo(set, app)
return set[app]
end
local function conditionExclude(set, app)
return not set[app]
end
local function curryCondition(condition, ...)
local appSet = listToSet({ ... })
local function curried(name)
return condition(appSet, name)
end
return curried
end
--- app select options
function allApps()
return true
end
function to(...)
return curryCondition(conditionTo, ...)
end
function exclude(...)
return curryCondition(conditionExclude, ...)
end
function any(...)
local conditions = { ... }
return function(currentAppName, currentTab)
for _, cond in ipairs(conditions) do
if cond(currentAppName, currentTab) then
return true
end
end
return false
end
end
function none(...)
local conditions = { ... }
return function(currentAppName, currentTab)
for _, cond in ipairs(conditions) do
if cond(currentAppName, currentTab) then
return false
end
end
return true
end
end
-- for patterns see https://www.lua.org/manual/5.1/manual.html#5.4.1
function toAppAndTab(appName,tabPattern)
local function condition(currentAppName, currentTab)
return currentAppName == appName and string.match(currentTab,tabPattern)
end
return condition
end
function toAppsAndTabs(...)
local conditions = {}
for _, pair in ipairs({...}) do
local appName, tabPattern = table.unpack(pair)
table.insert(conditions, toAppAndTab(appName, tabPattern))
end
return any(table.unpack(conditions))
end
-- for patterns see https://www.lua.org/manual/5.1/manual.html#5.4.1
function excludeAppAndTab(appName,tabPattern)
local function condition(currentAppName, currentTab)
return not (currentAppName == appName and string.match(currentTab,tabPattern))
end
return condition
end
function excludeAppsAndTabs(...)
local conditions = {}
for _, pair in ipairs({...}) do
local appName, tabPattern = table.unpack(pair)
table.insert(conditions, excludeAppAndTab(appName, tabPattern))
end
return none(table.unpack(conditions))
end
-- bindHotkey(AppCondition, modifier, key, function)
-- AppCondition := to(apps) | exclude(apps)
-- `apps` := can be a list of parameters or a table
-- bindHotkey(to("Google Chrome","code"), modifier, key, function)
-- bindHotkey(to({"Google Chrome","code"}), modifier, key, function)
-- bindHotkey(exclude("Google Chrome"), modifier, key, function)
-- bindHotkey(exclude("Google Chrome","whatsapp"), modifier, key, function)
-- bindHotkey(to("Google Chrome","IntelliJ IDEA"), {"cmd"}, "n", nil, myFunction)
-- bindHotkey(exclude("Google Chrome","IntelliJ IDEA"), {"cmd"}, "n", nil, myFunction)
-- bindHotkey(toAppAndTab("MailMate","pattern"), {"cmd"}, "n", nil, myFunction)
-- bindHotkey(toAppsAndTabs({"MailMate", "pattern"}, {"WhatsApp", "pattern"}), {"cmd"}, "n", nil, myFunction)
-- bindHotkey(any(toAppAndTab("MailMate", "pattern"), toAppAndTab("WhatsApp", "pattern")), {"cmd"}, "n", nil, myFunction)
-- bindHotkey(excludeAppAndTab("MailMate","pattern"), {"cmd"}, "n", nil, myFunction)
-- bindHotkey(none(excludeAppAndTab("MailMate","pattern"),excludeAppAndTab("WhatsApp", "pattern")), {"cmd"}, "n", nil, myFunction)
-- bindHotkey(excludeAppsAndTabs({"MailMate","pattern"},{"WhatsApp", "pattern"}), {"cmd"}, "n", nil, myFunction)
-- local apps = {"Google Chrome","IntelliJ IDEA"}
-- bindHotkey(to(apps), {"cmd"}, "n", nil, myFunction)
-- AllApps:
-- bindHotkey(allApps, {"cmd"}, "n", nil, myFunction)
function bindHotkey(runWithAppCondition, modifier, key, message, callback)
-- in case someone forget to set the message to nil
if not callback then
callback = message
message = nil
end
local hotkeyHandler
hotkeyHandler = hs.hotkey.bind(modifier, key, message, function()
local currentApp = hs.application.frontmostApplication():name()
local currentTab = hs.window.focusedWindow():title()
if runWithAppCondition(currentApp, currentTab) then
--hs.alert.show("execute on: " .. currentApp)
--logger.i("execute on: " .. currentApp)
callback()
else
--hs.alert.show("no function at: " .. currentApp)
--logger.i("no function at: " .. currentApp)
hotkeyDisableSilent(hotkeyHandler)
hs.eventtap.keyStroke(modifier, key)
hotkeyEnableSilent(hotkeyHandler)
end
end)
end
-- Bind Hotkey only to one app
function bindHotkeyOnlyTo(appname, modifier, key, message, callback)
-- in case someone forget to set the message to nil
if not callback then
callback = message
message = nil
end
local hotkeyHandler
hotkeyHandler = hs.hotkey.bind(modifier, key, message, function()
local currentApp = hs.application.frontmostApplication():name()
if (appname == currentApp) then
callback()
else
hotkeyDisableSilent(hotkeyHandler)
hs.eventtap.keyStroke(modifier, key)
hotkeyEnableSilent(hotkeyHandler)
end
end)
end