-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
304 lines (271 loc) · 8.89 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
--------------------------
-- CONFIGURATION --
--------------------------
-- GLOBAL
hyper = {"ctrl", "alt", "cmd"}
--------------------------
-- SpoonInstall --
--------------------------
-- Simplifies the installation of spoons
-- taken from here
-- https://zzamboni.org/post/my-hammerspoon-configuration-with-commentary/
hs.loadSpoon("SpoonInstall")
Install=spoon.SpoonInstall
--------------------------
-- App bindings --
--------------------------
-- Makes the switching from one app to another super easy
-- Taken from here
-- https://msol.io/blog/tech/work-more-efficiently-on-your-mac-for-developers/
local log = hs.logger.new('main', 'info')
-- focus on the last-focused window of every application given by name
function hyperFocusAll(key, ...)
hs.hotkey.bind(hyper, key, mkFocusByPreferredApplicationTitle(false, ...))
end
-- creates callback function to select application windows by application name
function mkFocusByPreferredApplicationTitle(stopOnFirst, ...)
local arguments = {...} -- create table to close over variadic args
return function()
local nowFocused = hs.window.focusedWindow()
local appFound = false
for _, app in ipairs(arguments) do
if stopOnFirst and appFound then break end
log:d('Searching for app ', app)
local application = hs.application.get(app)
if application ~= nil then
log:d('Found app', application)
local window = application:mainWindow()
if window ~= nil then
log:d('Found main window', window)
if window == nowFocused then
log:d('Already focused, moving on', application)
else
window:focus()
appFound = true
end
end
end
end
return appFound
end
end
-- focus on the last-focused window of the application given by name, or else launch it
function hyperFocusOrOpen(key, app)
local focus = mkFocusByPreferredApplicationTitle(true, app)
function focusOrOpen()
return (focus() or hs.application.launchOrFocus(app))
end
hs.hotkey.bind(hyper, key, focusOrOpen)
end
function setUpAppBindings()
hyperFocusOrOpen('w', 'Finder')
hyperFocusOrOpen('c', 'Visual Studio Code')
hyperFocusOrOpen('x', 'Firefox')
hyperFocusOrOpen('v', 'Telegram')
hyperFocusOrOpen('b', 'Rambox')
hyperFocusOrOpen('n', 'Mailspring')
hyperFocusOrOpen('s', 'spotify')
hyperFocusOrOpen('g', 'lepton')
hyperFocusOrOpen(',', 'iTerm2')
end
setUpAppBindings()
--------------------------
-- ClipboardTool --
--------------------------
-- Displays the content of the clipboard
function setUpClipboardTool()
ClipboardTool = hs.loadSpoon('ClipboardTool')
ClipboardTool.show_in_menubar = false
ClipboardTool:start()
ClipboardTool:bindHotkeys({
toggle_clipboard = {hyper, ";"}
})
end
setUpClipboardTool()
--------------------------
-- Caffeine --
--------------------------
-- One of the basic examples in the doc. Adds a menu in the toolbar giving the
-- opportunity to allow the machine to go to sleep or not
caffeine = hs.menubar.new()
function setCaffeineDisplay(state)
if state then
caffeine:setTitle("AWAKE")
else
caffeine:setTitle("SLEEPY")
end
end
function caffeineClicked()
setCaffeineDisplay(hs.caffeinate.toggle("displayIdle"))
end
if caffeine then
caffeine:setClickCallback(caffeineClicked)
setCaffeineDisplay(hs.caffeinate.get("displayIdle"))
end
--------------------------
-- Open folder --
--------------------------
-- Opens a given folder using a specific shortcut
local function directoryLaunchKeyRemap(mods, key, dir)
local mods = mods or {}
hs.hotkey.bind(mods, key, function()
local shell_command = "open " .. dir
hs.execute(shell_command)
end)
end
directoryLaunchKeyRemap(hyper, "1", "/Applications")
--------------------------
-- MenuHammer --
--------------------------
-- Add an overlay menu with shortcuts to programs
menuHammer = hs.loadSpoon("MenuHammer")
menuHammerMenuList = {
mainMenu = {
parentMenu = nil,
menuHotkey = {{'alt'}, 'space'},
menuItems = {
{cons.cat.action, '', 'W', 'Organize the files', {
{cons.act.shellcommand, "organize run"},
}},
{cons.cat.submenu, '', 'A', 'Applications', {
{cons.act.menu, "applicationMenu"}
}},
{cons.cat.action, '', 'T', "Terminal", {
{cons.act.launcher, 'Terminal'}
}},
{cons.cat.action, '', 'D', 'Desktop', {
{cons.act.launcher, 'Finder'},
{cons.act.keycombo, {'cmd', 'shift'}, 'd'},
}},
{cons.cat.action, '', 'E', "Split Safari/iTunes", {
{cons.act.func, function()
-- See Hammerspoon layout documentation for more info on this
local mainScreen = hs.screen{x=0,y=0}
hs.layout.apply({
{"Safari", nil, mainScreen, hs.layout.left50, nil, nil},
{"iTunes", nil, mainScreen, hs.layout.right50, nil, nil},
})
end }
}},
{cons.cat.action, '', 'H', "Hammerspoon Manual", {
{cons.act.func, function()
hs.doc.hsdocs.forceExternalBrowser(true)
hs.doc.hsdocs.moduleEntitiesInSidebar(true)
hs.doc.hsdocs.help()
end }
}},
{cons.cat.action, '', 'M', 'MenuHammer Default Config', {
{cons.act.openfile, "~/.hammerspoon/Spoons/MenuHammer.spoon/MenuConfigDefaults.lua"},
}},
{cons.cat.action, '', 'X', "Mute/Unmute", {
{cons.act.mediakey, "mute"}
}},
}
},
applicationMenu = {
parentMenu = "mainMenu",
menuHotkey = nil,
menuItems = {
{cons.cat.action, '', 'A', "App Store", {
{cons.act.launcher, 'App Store'}
}},
}
},
}
menuHammer:enter()
--------------------------
-- DeepLTranslate --
--------------------------
-- Opens a Deepl translation tool overlay. If you select text and wait
-- it will automatically fill out the text field
DeepLTranslate = hs.loadSpoon("DeepLTranslate")
hs.hotkey.bind(hyper, "d", function()
hs.alert.show("Pressed cmd + alt + ctrl + d")
DeepLTranslate:translateSelectionPopup()
end)
--------------------------
-- MountedVolumes --
--------------------------
-- Displays he available disk space for every mounted hard drive
-- as an overlay on the finder
MountedVolumes = hs.loadSpoon("MountedVolumes")
hs.hotkey.bind(hyper, "a", function()
hs.alert.show("Pressed cmd + alt + ctrl + a")
MountedVolumes:show()
end)
--------------------------
-- KSheet --
--------------------------
-- Generates a Cheatsheet for the current app
Install:andUse("KSheet",
{
hotkeys = {
toggle = { hyper, ":" }
}
}
)
--------------------------
-- Time Machine backup --
--------------------------
-- When plugging in a time machine harddrive
-- It displays the percentage of the backup progress in the menu bar
Install:andUse("TimeMachineProgress",
{
start = true
}
)
--------------------------
-- Shade --
--------------------------
-- Used to work in a dark environment ?
-- Shade allows to reduce the brightness of the screen
-- in a more helpful way than to actually reduce the screen brightness
hs.loadSpoon("Shade")
hs.hotkey.bind(hyper, "q", function()
hs.alert.show("Running shade")
Shade:toggleShade()
end)
--------------------------
-- HoldToQuit --
--------------------------
-- Tired of closing the apps by mistake ?
local obj = hs.loadSpoon("HoldToQuit")
obj:init()
obj:start()
--------------------------
-- Wifi Notifier --
--------------------------
-- Notifies you when the network changes
local obj = hs.loadSpoon("WifiNotifier")
obj:init()
obj:start()
--------------------------
-- MiroWindowsManager --
--------------------------
-- Very usefull to resize windows
hs.loadSpoon("MiroWindowsManager")
hs.window.animationDuration = 0.3
spoon.MiroWindowsManager:bindHotkeys({
up = {hyper, "up"},
right = {hyper, "right"},
down = {hyper, "down"},
left = {hyper, "left"},
fullscreen = {hyper, "f"}
})
--------------------------
-- reloadConfig --
--------------------------
-- Automatically reloads the hammerspoon config on save
function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
hs.reload()
end
end
myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")