-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
316 lines (277 loc) · 8.88 KB
/
init.lua
File metadata and controls
316 lines (277 loc) · 8.88 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
---------------------------------------------
-- Hot keys to quickly moving windows around
--------------------------------------------
-- move to occupy left half screen
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Left", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrame(f)
end)
-- move to occupy left right screen
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Right", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + (max.w / 2)
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrame(f)
end)
-- move to occupy full screen
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Up", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w
f.h = max.h
win:setFrame(f)
end)
-- Move the occupy the bottom half screen
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Down", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y + (max.h / 2)
f.w = max.w
f.h = max.h / 2
win:setFrame(f)
end)
-- Toggle fullscreen
-- hs.hotkey.bind({"cmd", "alt", "ctrl"}, "f", function()
-- local win = hs.window.focusedWindow()
-- win:toggleFullScreen()
-- end)
-- Send the window to next screen
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'n', function()
-- get the focused window
local win = hs.window.focusedWindow()
-- get the screen where the focused window is displayed, a.k.a. current screen
local screen = win:screen()
-- compute the unitRect of the focused window relative to the current screen
-- and move the window to the next screen setting the same unitRect
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
end)
--------------------------------------------
-- The following actions is similiar but leave
-- the windows other dimension unchanged
--------------------------------------------
-- Set the window width to half of the screen and move it to the left side
hs.hotkey.bind({"cmd", "ctrl"}, "Left", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.w = max.w / 2
win:setFrame(f)
end)
-- Set the window width to half of the screen and move it to the right side
hs.hotkey.bind({"cmd", "ctrl"}, "Right", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + f.w
f.w = max.w / 2
win:setFrame(f)
end)
-- Set the window height to half of the screen and move it to the top
hs.hotkey.bind({"cmd", "ctrl"}, "Up", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.y = max.y
f.h = max.h / 2
win:setFrame(f)
end)
-- Set the window height to half of the screen and move it to the bottom
hs.hotkey.bind({"cmd", "ctrl"}, "Down", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.h = max.h / 2
f.y = max.y + f.h
win:setFrame(f)
end)
-------------------------------------------------------
-- send window to a specific screen and then fullscreen
-------------------------------------------------------
local function sendToFullscreen()
local win = hs.window.focusedWindow()
local focusScreen = hs.screen{x=-1,y=0}
local numScreens = #hs.screen.allScreens()
if numScreens == 3 then
win:moveToScreen(focusScreen)
end
hs.timer.doAfter(0.2, function()
win:setFullScreen(true)
end)
end
hs.hotkey.bind({"cmd", "ctrl"}, "F", sendToFullscreen)
-------------------------------------------------------
-- send iTerm to a specific screen and then fullscreen
-------------------------------------------------------
hs.hotkey.bind({"cmd", "ctrl"}, "I", function()
local win = hs.window.get("iTerm2")
local focusScreen = hs.screen{x=-1,y=0}
local numScreens = #hs.screen.allScreens()
if numScreens == 3 then
win:moveToScreen(focusScreen)
end
hs.timer.doAfter(0.2, function()
win:setFullScreen(true)
end)
end)
-----------------------------------------------
-- default layouts to fit in mulitple monitors
-- if there are more than one
-----------------------------------------------
local positions = {
leftTop = {x=0, y=0, w=0.5, h=0.5},
leftBottom = {x=0, y=0.5, w=0.5, h=0.5},
rightTop = {x=0.5, y=0, w=0.5, h=0.5},
rightBottom = {x=0.5, y=0.5, w=0.5, h=0.5}
}
local appNames = {
"Google Chrome",
"VS Code @ FB",
"Microsoft Outlook",
"Workplace Chat",
"iTerm",
"Asana"
}
-- first by resolution then by x
local function screenCompare(a, b)
-- first compare the area then the position
aRect = a:fullFrame()
bRect = b:fullFrame()
if aRect['h']*aRect['w'] == bRect['h']*bRect['w'] then
return aRect['x'] < bRect['x']
end
return aRect['h']*aRect['w'] < bRect['h']*bRect['w']
end
-- auto lanch the list apps
local function launchApps()
for i, appName in ipairs(appNames) do
hs.application.launchOrFocus(appName)
end
end
-- apply a predefined layout based on the number of monitors
function switchLayout()
local allScreens = hs.screen.allScreens()
table.sort(allScreens, screenCompare)
chrome_windows = hs.application.get('Google Chrome'):allWindows()
local numScreens = #allScreens
local layout = {}
if numScreens == 1 then
layout = layoutSingleScreen
elseif numScreens == 2 then
layout = {
{"VS Code @ FB", nil, allScreens[2], hs.layout.maximized, nil, nil},
{"iTerm2", nil, allScreens[1], hs.layout.left50, nil, nil},
{"Workplace Chat", nil, allScreens[1], hs.layout.left50, nil, nil},
{"Asana", nil, allScreens[1], hs.layout.right50, nil, nil},
{"Microsoft Outlook", nil, allScreens[1], positions.full, nil, nil},
}
-- Chrome always goes to the larger screen
for k, g in pairs(chrome_windows) do
layout[#layout+1] = {"Google Chrome", g, allScreens[2], hs.layout.maximized, nil, nil}
end
elseif numScreens == 3 then
layout = {
{"VS Code @ FB", nil, allScreens[3], hs.layout.maximized, nil, nil},
{"iTerm2", nil, allScreens[1], hs.layout.maximized, nil, nil},
{"Workplace Chat", nil, allScreens[1], hs.layout.left50, nil, nil},
{"Asana", nil, allScreens[1], hs.layout.right50, nil, nil},
{"Microsoft Outlook", nil, allScreens[1], hs.layout.maximized, nil, nil},
}
-- Add all chrome windows to the most left largest screen
for k, g in pairs(chrome_windows) do
layout[#layout+1] = {"Google Chrome", g, allScreens[2], hs.layout.maximized, nil, nil}
end
end
hs.layout.apply(layout)
end
-- create a keybinding for auto applying layouts
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "1", function()
switchLayout()
end)
-------------------------------------------------
-- create a menu for easy of handeling: disabled
-------------------------------------------------
local menu = hs.menubar.new()
local function enableMenu()
menu:setTitle("🖥")
menu:setTooltip("No Layout")
menu:setMenu({
{ title = "Launch Apps", fn = launchApps },
{ title = "Set Screen Layout", fn = switchLayout }
})
end
-- enableMenu()
-------------------------------------------------
-- Some short key to bring up the applications
-------------------------------------------------
local hyper = {"cmd", "alt", "ctrl"}
local applicationHotkeys = {
e = 'Google Chrome',
f = 'Finder',
t = 'iTerm',
v = 'Code',
w = 'Microsoft Word',
s = 'Stocks',
b = 'BBEdit',
o = 'Obsidian',
l = 'Calendar',
p = "Preview",
m = "MacVim",
z = "Zoom Meeting",
a = "Voice Memos"
}
-- launch focus or rotate application
local function launchOrFocusOrRotate(app)
hs.application.enableSpotlightForNameSearches(true)
local focusedWindow = hs.window.focusedWindow()
-- If already focused, try to find the next window
if focusedWindow and focusedWindow:application():name() == app then
local appWindows = hs.application.get(app):allWindows()
if #appWindows > 0 then
-- It seems that this list order changes after one window get focused,
-- let's directly bring the last one to focus every time
appWindows[#appWindows]:focus()
else -- should not happen, but just to make sure
hs.application.launchOrFocus(app)
end
else -- not focused
if app == 'Code' then
app = 'Visual Studio Code'
elseif app == "Voice Memos" then
hs.osascript.applescript([[
tell application "Voice Memos"
activate
end tell
]])
end
hs.application.launchOrFocus(app)
end
end
for key, app in pairs(applicationHotkeys) do
hs.hotkey.bind(hyper, key, function()
launchOrFocusOrRotate(app)
end)
end