/
init.lua
128 lines (117 loc) · 3.21 KB
/
init.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
--- === MicMute ===
---
--- Microphone Mute Toggle and status indicator
---
--- Download: [https://github.com/Hammerspoon/Spoons/raw/master/Spoons/MicMute.spoon.zip](https://github.com/Hammerspoon/Spoons/raw/master/Spoons/MicMute.spoon.zip)
local obj={}
obj.__index = obj
-- Metadata
obj.name = "MicMute"
obj.version = "1.1"
obj.author = "dctucker <dctucker@github.com>"
obj.homepage = "https://dctucker.com"
obj.license = "MIT - https://opensource.org/licenses/MIT"
function obj:updateMicMute(muted)
if muted == -1 then
muted = hs.audiodevice.defaultInputDevice():muted()
end
if muted then
obj.mute_menu:setTitle("📵 Muted")
else
obj.mute_menu:setTitle("🎙 On")
end
end
--- MicMute:toggleMicMute()
--- Method
--- Toggle mic mute on/off
---
--- Parameters:
--- * None
function obj:toggleMicMute()
local mic = hs.audiodevice.defaultInputDevice()
local zoom = hs.application'Zoom'
local teams = hs.application.find("com.microsoft.teams")
if mic:muted() then
mic:setInputMuted(false)
if zoom then
local ok = zoom:selectMenuItem'Unmute Audio'
if not ok then
hs.timer.doAfter(0.5, function()
zoom:selectMenuItem'Unmute Audio'
end)
end
end
if teams then
local ok = teams:selectMenuItem'Unmute'
if not ok then
hs.timer.doAfter(0.5, function()
hs.eventtap.keyStroke({"cmd","shift"}, "m", 0, teams)
end)
end
end
else
mic:setInputMuted(true)
if zoom then
local ok = zoom:selectMenuItem'Mute Audio'
if not ok then
hs.timer.doAfter(0.5, function()
zoom:selectMenuItem'Mute Audio'
end)
end
end
if teams then
local ok = teams:selectMenuItem'Mute'
if not ok then
hs.timer.doAfter(0.5, function()
hs.eventtap.keyStroke({"cmd","shift"}, "m", 0, teams)
end)
end
end
end
obj:updateMicMute(-1)
end
--- MicMute:bindHotkeys(mapping, latch_timeout)
--- Method
--- Binds hotkeys for MicMute
---
--- Parameters:
--- * mapping - A table containing hotkey modifier/key details for the following items:
--- * toggle - This will cause the microphone mute status to be toggled. Hold for momentary, press quickly for toggle.
--- * latch_timeout - Time in seconds to hold the hotkey before momentary mode takes over, in which the mute will be toggled again when hotkey is released. Latch if released before this time. 0.75 for 750 milliseconds is a good value.
function obj:bindHotkeys(mapping, latch_timeout)
if (self.hotkey) then
self.hotkey:delete()
end
local mods = mapping["toggle"][1]
local key = mapping["toggle"][2]
if latch_timeout then
self.hotkey = hs.hotkey.bind(mods, key, function()
self:toggleMicMute()
self.time_since_mute = hs.timer.secondsSinceEpoch()
end, function()
if hs.timer.secondsSinceEpoch() > self.time_since_mute + latch_timeout then
self:toggleMicMute()
end
end)
else
self.hotkey = hs.hotkey.bind(mods, key, function()
self:toggleMicMute()
end)
end
return self
end
function obj:init()
obj.time_since_mute = 0
obj.mute_menu = hs.menubar.new()
obj.mute_menu:setClickCallback(function()
obj:toggleMicMute()
end)
obj:updateMicMute(-1)
hs.audiodevice.watcher.setCallback(function(arg)
if string.find(arg, "dIn ") then
obj:updateMicMute(-1)
end
end)
hs.audiodevice.watcher.start()
end
return obj