This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
speech.lua
225 lines (195 loc) · 8.07 KB
/
speech.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
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
--
-- hold down fn, even when on, or command is ignored (minimizes false positives in noisy
-- environments.)
--
local module, placeholder = {}, {}
local speech = require("hs.speech")
local listener = speech.listener
local fnutils = require("hs.fnutils")
local log = require("hs.logger").new("mySpeech","warning")
local settings = require("hs.settings")
local eventtap = require("hs.eventtap")
local watchable = require"hs.watchable"
local window = require"hs.window"
local hue = require("hs._asm.hue")
local timer = require("hs.timer")
local commands = {}
local title = "Hammerspoon"
local listenerCallback = function(listenerObj, text)
if eventtap.checkKeyboardModifiers().fn then
if commands[text] then
commands[text]()
else
log.wf("Unrecognized command '%s' received", text)
end
else
log.vf("FN not depressed -- ignoring command '%s'", text)
end
end
local updateCommands = function()
if module.recognizer then
local cmdList = {}
for k,v in fnutils.sortByKeys(commands) do
table.insert(cmdList, k)
end
module.recognizer:commands(cmdList)
end
end
module.log = log
module.commands = commands
module.watchables = watchable.new("utils.speech")
module.watchables.isListening = false
module.add = function(text, func)
assert(type(text) == "string", "command must be a string")
assert(type(func) == "function", "action must be a function")
if commands[text] then
error("Command '"..text.."' is already registered", 2)
end
commands[text] = func
updateCommands()
return placeholder
end
module.remove = function(text)
assert(type(text) == "string", "command must be a string")
if commands[text] then
commands[text] = nil
else
error("Command '"..text.."' is not registered", 2)
end
updateCommands()
return placeholder
end
module.start = function()
updateCommands() -- should be current, but just in case
module.recognizer:title(title):start()
settings.set("_asm.listener", true)
if (module.listenLabel) then
local screen = require("hs.screen").primaryScreen():fullFrame()
module.listenLabel:show():setFrame{
x = screen.x + 5, y = screen.y + screen.h - 21,
h = 14, w = 150
}
end
module.watchables.isListening = true
return placeholder
end
module.stop = function()
module.recognizer:title("Disabled: "..title):stop()
if (module.listenLabel) then module.listenLabel:hide() end
module.watchables.isListening = false
return placeholder
end
module.isListening = function()
if module.recognizer then
return module.recognizer:isListening()
else
return nil
end
end
module.disableCompletely = function()
if module.recognizer then
module.recognizer:delete()
end
module.recognizer = nil
setmetatable(placeholder, nil)
if (module.listenLabel) then
module.listenLabel = module.listenLabel:delete()
end
settings.set("_asm.listener", false)
module.watchables.isListening = false
end
module.init = function()
if module.recognizer then
error("Listener already initialized", 2)
end
module.recognizer = listener.new(title):setCallback(listenerCallback)
:foregroundOnly(false)
:blocksOtherRecognizers(false)
local screen = require("hs.screen").primaryScreen():fullFrame()
module.listenLabel = require("hs.drawing").text({
x = screen.x + 5, y = screen.y + screen.h - 21,
h = 14, w = 150
}, require("hs.styledtext").new("Hold FN while speaking...", {
font = { name = "Menlo-Italic", size = 10 },
color = { list = "Crayons", name = "Sky" },
paragraphStyle = { lineBreak = "clip" }
})):setBehaviorByLabels{"canJoinAllSpaces"}
:setLevel("popUpMenu")
:show()
return setmetatable(placeholder, {
__index = function(_, k)
if module[k] then
if type(module[k]) ~= "function" then return module[k] end
return function(_, ...) return module[k](...) end
else
return nil
end
end,
__tostring = function(_) return module.recognizer:title() end,
__pairs = function(_) return pairs(module) end,
})
end
placeholder.init = function() return module.init() end
module.add("Hammerspoon Console", hs.openConsole)
module.add("System Console", function() require("hs.application").launchOrFocus("Console") end)
-- module.add("Open Editor", function() require("hs.application").launchOrFocus("BBEdit") end)
-- module.add("Open Browser", function() require("hs.application").launchOrFocus("Safari") end)
-- module.add("Open SmartGit", function() require("hs.application").launchOrFocus("SmartGit") end)
-- module.add("Open Mail", function() require("hs.application").launchOrFocus("Mail") end)
module.add("Terminal", function() require("hs.application").launchOrFocus("Terminal") end)
local extraCommandsTime = 10
local lightsOnFunction
lightsOnFunction = function()
module.remove("Lights")
module.add("Lights", function() end)
module.add("On", function()
for i, v in ipairs(hue.default:paths("lights", { on = false })) do hue.default:put(v .. "/state", { on = true }) end
hue.default:put(hue.default:paths("lights", { type = "color" }, true)[1] .. "/state", { effect="none" })
hue.default:put(hue.default:paths("lights", { type = "color" }, true)[1] .. "/state", { ct = 366 })
module._lightTimer:setNextTrigger(extraCommandsTime)
end)
module.add("Off", function()
for i, v in ipairs(hue.default:paths("lights", { on = true })) do hue.default:put(v .. "/state", { on = false }) end
module._lightTimer:setNextTrigger(extraCommandsTime)
end)
module.add("Dimmer", function()
for i, v in ipairs(hue.default:paths("lights", { on = true })) do hue.default:put(v .. "/state", { bri_inc = -64 }) end
module._lightTimer:setNextTrigger(extraCommandsTime)
end)
module.add("Brighter", function()
for i, v in ipairs(hue.default:paths("lights", { on = true })) do hue.default:put(v .. "/state", { bri_inc = 64 }) end
module._lightTimer:setNextTrigger(extraCommandsTime)
end)
module.add("Night Mode", function()
for i, v in ipairs(hue.default:paths("lights", { on = true })) do hue.default:put(v .. "/state", { on = false }) end
hue.default:put(hue.default:paths("lights", { type = "color" }, true)[1] .. "/state", { on = true, bri = 32, sat = 254, effect="colorloop" })
module._lightTimer:setNextTrigger(extraCommandsTime)
end)
module._lightTimer = timer.doAfter(extraCommandsTime, function()
module._lightTimer:stop()
module._lightTimer = nil
module.remove("Lights")
module.add("Lights", lightsOnFunction)
module.remove("On")
module.remove("Off")
module.remove("Dimmer")
module.remove("Brighter")
module.remove("Night Mode")
end)
end
module.add("Lights", lightsOnFunction)
module.add("Re-Load Hammerspoon", hs.reload)
module.add("Re-Launch Hammerspoon", _asm.relaunch)
module.add("Toggle Command List", function()
local axuielement = require"hs._asm.axuielement"
local dictationWindows = fnutils.ifilter(window.allWindows(), function(_)
return _:role() == "AXButton" and _:application():name() == "Dictation"
end)
local target = (#dictationWindows == 1) and dictationWindows[1]
or fnutils.find(dictationWindows, function(_) return _:subrole() == "AXCloseButton" end)
axuielement.windowElement(target):doPress()
end)
-- module.add("Stop Listening", module.stop)
module.add("Go away for a while", module.disableCompletely)
if settings.get("_asm.listener") then placeholder.init():start() end
return placeholder