-
Notifications
You must be signed in to change notification settings - Fork 0
/
hammerspoon.lua
248 lines (202 loc) · 7.48 KB
/
hammerspoon.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
-- Initialize the configuration
if _G.config == nil then _G.config = {} end
-- Style options
hs.alert.defaultStyle.radius = 3
hs.alert.defaultStyle.strokeColor.alpha = 0
hs.window.animationDuration = 0
-- Move the focused window to a target position on the current screen
local function moveTo(target)
local window = hs.window.frontmostWindow()
local frame = window:frame()
local screen = window:screen():frame()
if target == 1 then
-- Quadrant 1 (Top-left)
frame.x = screen.x
frame.y = screen.y
elseif target == 2 then
-- Quadrant 2 (Top-right)
frame.x = screen.x + screen.w - frame.w
frame.y = screen.y
elseif target == 3 then
-- Quadrant 3 (Bottom-right)
frame.x = screen.x + screen.w - frame.w
frame.y = screen.y + screen.h - frame.h
elseif target == 4 then
-- Quadrant 4 (Bottom-left)
frame.x = screen.x
frame.y = screen.y + screen.h - frame.h
end
window:setFrame(frame)
end
-- Control the volume of the active audio device
local function setVolume(target, message)
local audioDevice = hs.audiodevice.defaultOutputDevice()
if target == 0 then
audioDevice:setMuted(not audioDevice:muted())
if message then hs.alert.show(audioDevice:muted() and message[1] or message[2]) end
else
audioDevice:setVolume(target * 100)
audioDevice:setMuted(false)
if message then hs.alert.show(message) end
end
end
-- Microphone mute
local micMuteIcon = hs.menubar.new(false)
local function toggleMicMute()
local audioDevice = hs.audiodevice.defaultInputDevice()
audioDevice:setInputMuted(not audioDevice:inputMuted())
if audioDevice:inputMuted() then
micMuteIcon:returnToMenuBar()
micMuteIcon:setTitle('Mute')
micMuteIcon:setTooltip('Microphone is muted')
micMuteIcon:setClickCallback(toggleMicMute)
else
micMuteIcon:removeFromMenuBar()
end
end
-- Caffeine
local caffeineIcon = hs.menubar.new(false)
local function toggleCaffeine()
if hs.caffeinate.toggle('displayIdle') then
caffeineIcon:returnToMenuBar()
caffeineIcon:setTitle('☕️')
caffeineIcon:setTooltip('Prevent display from sleeping')
caffeineIcon:setClickCallback(toggleCaffeine)
else
caffeineIcon:removeFromMenuBar()
end
end
-- Clipboard Manager
local clipboardManager = hs.loadSpoon('ClipboardTool')
clipboardManager.hist_size = 50
clipboardManager.show_copied_alert = false
clipboardManager.show_in_menubar = false
clipboardManager:bindHotkeys({ toggle_clipboard = { { 'ctrl' }, 'space' } })
clipboardManager:start()
-- AutoClicker
local autoClickerInterval = 0.1
local autoClickerIcon = hs.menubar.new(false)
local isAutoClicking = false
local function toggleAutoClicker()
if isAutoClicking then
autoClickerIcon:removeFromMenuBar()
isAutoClicking = false
else
isAutoClicking = true
autoClickerIcon:returnToMenuBar()
autoClickerIcon:setTitle('🐭')
autoClickerIcon:setClickCallback(toggleAutoClicker)
hs.timer.doWhile(
function() return isAutoClicking end,
function() hs.eventtap.leftClick(hs.mouse.absolutePosition(), autoClickerInterval * 0.5) end,
autoClickerInterval
)
end
end
-- Safari mouse bindings
_G.watchers = {}
local safariMouseEventWatcher = hs.eventtap.new({ hs.eventtap.event.types.otherMouseDown }, function(tapEvent)
local buttonIndex = tapEvent:getProperty(hs.eventtap.event.properties.mouseEventButtonNumber)
if buttonIndex == 3 then
hs.eventtap.keyStroke({ 'cmd' }, '[')
elseif buttonIndex == 4 then
hs.eventtap.keyStroke({ 'cmd' }, ']')
end
end)
_G.watchers.safariApplicationWatcher = hs.application.watcher.new(function(applicationName, eventType, application)
if applicationName ~= 'Safari' then return end
if eventType == hs.application.watcher.activated then
safariMouseEventWatcher:start()
elseif eventType == hs.application.watcher.deactivated then
safariMouseEventWatcher:stop()
end
end)
_G.watchers.safariApplicationWatcher:start()
-- Hotkey bindings
local mods = { 'ctrl', 'cmd' }
hs.hotkey.bind(mods, '1', function() moveTo(1) end)
hs.hotkey.bind(mods, '2', function() moveTo(2) end)
hs.hotkey.bind(mods, '3', function() moveTo(3) end)
hs.hotkey.bind(mods, '4', function() moveTo(4) end)
hs.hotkey.bind(mods, 'C', function() hs.window.frontmostWindow():centerOnScreen() end)
hs.hotkey.bind(mods, 'M', function() hs.window.frontmostWindow():maximize() end)
hs.hotkey.bind(
mods,
'N',
function() hs.window.frontmostWindow():moveToUnit({ x = 1 / 4, y = 1 / 4, w = 1 / 2, h = 1 / 2 }) end
)
hs.hotkey.bind(
mods,
'B',
function() hs.window.frontmostWindow():moveToUnit({ x = 1 / 8, y = 1 / 8, w = 3 / 4, h = 3 / 4 }) end
)
hs.hotkey.bind(mods, 'H', function() hs.window.frontmostWindow():moveToUnit(hs.layout.left50) end)
hs.hotkey.bind(mods, 'L', function() hs.window.frontmostWindow():moveToUnit(hs.layout.right50) end)
hs.hotkey.bind(mods, '[', function() hs.window.frontmostWindow():moveOneScreenWest() end)
hs.hotkey.bind(mods, ']', function() hs.window.frontmostWindow():moveOneScreenEast() end)
hs.hotkey.bind(mods, '0', function() setVolume(0, { 'Mute', 'Unmute' }) end)
hs.hotkey.bind(mods, '-', function() setVolume(2 / 16, 'Volume low') end)
hs.hotkey.bind(mods, '=', function() setVolume(8 / 16, 'Volume normal') end)
hs.hotkey.bind(mods, 'delete', function() toggleMicMute() end)
hs.hotkey.bind(mods, 'A', toggleAutoClicker)
hs.hotkey.bind(mods, 'Z', toggleCaffeine)
hs.hotkey.bind(mods, 'R', function() hs.reload() end)
hs.hotkey.bind(mods, 'I', function() hs.application.launchOrFocusByBundleID('com.apple.Safari') end)
-- Conditional hotkey bindings
hs.hotkey.bind(mods, 'P', function() print(hs.application.frontmostApplication():bundleID()) end)
hs.hotkey.bind(
'cmd',
'escape',
function() hs.application.launchOrFocusByBundleID(_G.config.terminal and _G.config.terminal or 'com.apple.Terminal') end
)
if _G.config.browser then
hs.hotkey.bind(mods, 'O', function() hs.application.launchOrFocusByBundleID(_G.config.browser) end)
end
if _G.config.quickLaunch then
hs.hotkey.bind(mods, '\\', function()
for _, bundleID in ipairs(_G.config.quickLaunch) do
hs.application.launchOrFocusByBundleID(bundleID)
end
end)
end
if _G.config.shortcuts then
local function sendText(text)
local delimiterMap = { ['\n'] = 'return', ['\t'] = 'tab' }
local delimiterRegex = ''
for key in pairs(delimiterMap) do
delimiterRegex = delimiterRegex .. key
end
local delimiters = {}
for delimiter in string.gmatch(text, '[' .. delimiterRegex .. ']') do
table.insert(delimiters, delimiterMap[delimiter])
end
local i = 1
for substring in string.gmatch(text, '[^' .. delimiterRegex .. ']*') do
hs.eventtap.keyStrokes(substring)
if delimiters[i] then hs.eventtap.keyStroke({}, delimiters[i]) end
i = i + 1
end
end
local shortcutChooser = hs.chooser.new(function(choice)
if choice then
if choice.keyStrokes then
if type(choice.keyStrokes) == 'string' then sendText(choice.keyStrokes) end
if type(choice.keyStrokes) == 'table' then
if choice.interval then
for i, text in ipairs(choice.keyStrokes) do
hs.timer.doAfter(choice.interval * (i - 1), function() sendText(text) end)
end
else
sendText(table.concat(choice.keyStrokes, ''))
end
end
end
if _G[choice.callback] ~= nil then _G[choice.callback]() end
end
end)
shortcutChooser:choices(_G.config.shortcuts)
hs.hotkey.bind(mods, ';', function()
shortcutChooser:query(nil)
shortcutChooser:show()
end)
end