-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
180 lines (152 loc) · 4.71 KB
/
init.lua
File metadata and controls
180 lines (152 loc) · 4.71 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
require 'utils'
require 'action'
require 'hardware'
cfg = require 'config'
---------------------
-- Reload config --
---------------------
function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
if hardware then
hardware.stop()
end
if screenWatcher then
screenWatcher:stop()
end
if appWatcher then
appWatcher:stop()
end
configWatcher:stop()
hs.reload()
hs.notify.new( {title='Hammerspoon', subTitle='Configuration reloaded'} ):send()
end
end
configWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig)
configWatcher:start()
if not cfg then
error("Missing 'config.lua'. Copy and edit 'example-config.lua' to create your own configuration.")
end
---------------------------
-- Application Watcher --
---------------------------
function arrangeOrWait(app, layout, tries)
return function()
if tries > 0 and #app:allWindows() == 0 then
hs.timer.doAfter(0.1, arrangeOrWait(app, layout, tries -1 ))
else
arrange(app, layout)
end
end
end
function applicationWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
if (appName == "Finder") then
appObject:selectMenuItem({"Window", "Bring All to Front"})
end
end
-- layout a launched app if it's in the layout list
if (eventType == hs.application.watcher.launched) then
local layout = currentLayout()
if layout[appName] then
local app = hs.application.find(appName)
arrangeOrWait(app, layout, 30)() -- will wait for max 3 seconds
end
end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()
--------------------
-- Window Layout --
--------------------
function arrange(app, layout)
local actions = layout[app:title()]
local mainWindow = app:mainWindow()
if not actions or not mainWindow or not mainWindow:isStandard() then return end
for _, action in pairs(actions) do action(mainWindow) end
end
function currentLayout()
local layout = cfg.singleLayout
if (#hs.screen.allScreens() > 1) then
layout = cfg.dualLayout
end
return layout
end
function layout()
for _, app in pairs(hs.application.runningApplications()) do arrange(app, currentLayout()) end
end
function dumpLayout()
local layout = "local myLayout = {\n"
local first = true
for _, app in pairs(hs.application.runningApplications()) do
local mainWindow = app:mainWindow()
if mainWindow and mainWindow:isStandard() then
if not first then
layout = layout .. ",\n"
end
first = false
local frame = mainWindow:frame()
layout = layout .. string.format(" [\"%s\"] = { Action.Frame(%d, %d, %d, %d) }", app:title(), frame.x, frame.y, frame.w, frame.h)
end
end
layout = layout .. "\n}"
hs.pasteboard.setContents(layout)
print(layout)
end
----------------------
-- Screen Watcher --
----------------------
function screenWatcher()
if cfg.manageBluetooth then
local blueutil = "/usr/local/bin/blueutil"
if (#hs.screen.allScreens() == 1) then
os.execute(blueutil .. " -p 0")
else
os.execute(blueutil .. " -p 1")
end
end
layout()
end
screenWatcher = hs.screen.watcher.new(screenWatcher)
screenWatcher:start()
---------------------------------------------
-- hammerspoon://devopen?url=http://.... --
---------------------------------------------
hs.urlevent.bind("devopen", function(event, params)
local url = params.url
local browser = utils.focusBrowser()
hs.eventtap.keyStroke({ "cmd" }, "n")
hs.timer.doAfter(1.0, function()
browser:activate()
hs.eventtap.keyStroke({ "cmd" }, "l")
hs.eventtap.keyStrokes(url)
hs.eventtap.keyStroke( {}, "return")
local window = browser:focusedWindow()
window:moveOneScreenWest() window:moveToUnit(hs.layout.right75)
end)
end)
------------------------------------------------------------
-- hammerspoon://notify?title=...&subtitle=...&info=... --
------------------------------------------------------------
hs.urlevent.bind("notify", function(event, params)
utils.notify(params.title, params.subtitle, params.info)
end)
------------------
-- Keymapings --
------------------
hs.hotkey.bind(cfg.mash, 'l', layout)
hs.hotkey.bind(cfg.mash, 'r', hs.reload)
hs.hotkey.bind(cfg.mash, 'd', dumpLayout)
hs.hotkey.bind(cfg.mash, 'b', function() hardware.showBatteryStatus(true) end)
hs.hotkey.bind(cfg.mash, "h", hs.hints.windowHints)
hs.hotkey.bind(cfg.mash, "m", function()
-- insert email address at current cursor position
hs.eventtap.keyStrokes(cfg.email)
end)
hardware.start() -- enable hardware notifications
layout() -- layout windows after start