-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
290 lines (247 loc) · 9.79 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
--[[
---------- HAMMERSPOON CONFIGURATION ------------
Description: A lua approach to speed up your workflow
by: Danilo Toapanta
---------------- RESOURCES ----------------------
-- To able to use Apple script
hs.osascript.applescript()
-- Source how to open New Tab Safari next to current
https://www.cultofmac.com/691905/how-to-force-safari-tabs-open-at-end-of-tab-bar/
-- Closes Tab, but depricated due to lag
-- Used in function: close()
hs.appfinder.appFromName("Safari"):activate()
local browser = hs.appfinder.appFromName("Safari")
local menu_item = browser:findMenuItem({ "File", "Close Tab" })
-- If Close Tab still enabled
if (menu_item['enabled'] == false) then
browser:selectMenuItem({ "File", "Close Window" })
else
-- When only one tab remaining, close it
browser:selectMenuItem({ "File", "Close Window" })
end
-- Docs
https://www.hammerspoon.org/docs/hs.eventtap.html#newKeyEvent
https://www.hammerspoon.org/docs/hs.hotkey.html#new
https://www.hammerspoon.org/docs/hs.eventtap.event.html#newKeyEventSequence
https://www.hammerspoon.org/docs/hs.application.html#name
-- Open Tabs
https://zzamboni.org/post/just-enough-lua-to-be-productive-in-hammerspoon-part-2/
]]
-- Autoreload Configuration
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
hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("New changes applied")
--- Open App
function open(name)
return function()
hs.application.launchOrFocus(name)
if name == 'Finder' then
hs.appfinder.appFromName(name):activate()
end
end
end
--- Close App
function closeApp(appName)
return function()
local app = hs.application.find(appName)
if app then
app:kill()
else
hs.alert.show(appName .. " is not running")
end
end
end
-- Closes a tab of Safari if able else send close keybinding to system
function close()
return function()
local focus_window = hs.window.focusedWindow()
local name_app_focus_window = focus_window:application():name()
-- Only close option for Safari
if (name_app_focus_window == "Safari") then
-- Closes only one Tab from Safari
hs.osascript.applescript([[
tell application "Safari"
set tabcount to number of tabs in window 1
-- display dialog tabcount
if tabcount = 1 then
close window 1
else
-- close tab of window 1
tell application "System Events"
tell process "Safari"
-- activate
set frontmost to true
click menu item "Close Tab" of menu "File" of menu bar 1
end tell
end tell
end if
end tell
]])
-- For remainder Apps use Cmd+Q
else
hs.osascript.applescript([[
-- tell application "System Events"
-- set activeApp to name of first application process whose frontmost is true
-- -- display dialog activeApp
-- tell application activeApp
-- tell application "System Events" to keystroke "Q" using command down
-- -- keystroke "Q" using command down
-- end tell
-- end tell
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
end tell
display dialog activeApp
tell application "System Events" to ¬
tell application process activeApp
keystroke "Q" using command down
end tell
]])
-- hs.eventtap.keyStroke({"cmd"}, "Q", 0, hs.application.frontmostApplication() )
end
end
end
--- Open Microsoft Edge New Tab
function open_NewTab(webbrowser)
return function()
local browser = hs.appfinder.appFromName(webbrowser)
browser:activate()
local str_menu_item = { "File", "New Tab" }
local menu_item = browser:findMenuItem(str_menu_item)
if (menu_item) then
browser:selectMenuItem(str_menu_item)
--hs.alert.show(hs.application.frontmostApplication():name())
end
end
end
--- Open Microsoft Edge New Window
function open_NewWindow(webbrowser)
return function()
local browser = hs.appfinder.appFromName(webbrowser)
browser:activate()
local str_menu_item = { "File", "New Window" }
local menu_item = browser:findMenuItem(str_menu_item)
if (menu_item) then
browser:selectMenuItem(str_menu_item)
end
end
end
-- Opens URL
local URL = "https://github.com/danilotpnta?tab=repositories"
function open_url(URL)
return function()
hs.urlevent.openURL(URL)
end
end
-- Binding Keys
-- hs.hotkey.bind({ "cmd" }, "E", open("Finder"))
-- hs.hotkey.bind({ "cmd" }, "Y", open("Youtube"))
-- hs.hotkey.bind({ "cmd" }, "T", open("Google Translate"))
-- hs.hotkey.bind({ "cmd","⌥" }, "D", open("Google Docs"))
-- hs.hotkey.bind({ "cmd","⌥" }, "S", open("Google Sheets"))
-- hs.hotkey.bind({ "cmd" }, "D", open("Google Drive"))
-- hs.hotkey.bind({ "cmd" }, "1", open_url(URL))
-- hs.hotkey.bind({ "cmd" }, "2", open("Gmail Danilo"))
-- hs.hotkey.bind({ "cmd" }, "3", open("Microsoft Outlook"))
-- hs.hotkey.bind({ "cmd" }, "K", open("Google Keep"))
-- hs.hotkey.bind({ "cmd" }, "M", open("Google Maps"))
-- hs.hotkey.bind({ "cmd", "option" }, "C", open("Google Calendar"))
-- hs.hotkey.bind({ "cmd" }, "W", open("WhatsApp"))
-- hs.hotkey.bind({ "cmd"}, "X", open_NewTab("Safari"))
-- hs.hotkey.bind({ "cmd" }, "X", open_NewTab("Microsoft Edge"))
-- hs.hotkey.bind({ "cmd" }, "X", open_NewWindow("Microsoft Edge"))
-- local focus_window = hs.window.focusedWindow()
-- local name_app_focus_window = focus_window:application():name()
-- hs.alert.show(name_app_focus_window)
-- Only close option for Safari
if (name_app_focus_window == "Safari") then
-- hs.hotkey.bind({ "cmd"}, "Q", close())
createManagedHotkey({ "cmd"}, "Q", close()):enable()
end
close_app = hs.hotkey.new('⌘', 'q', function()
hs.osascript.applescript([[
tell application "Safari"
set tabcount to number of tabs in window 1
-- display dialog tabcount
if tabcount = 1 then
close window 1
else
-- close tab of window 1
tell application "System Events"
tell process "Safari"
-- activate
set frontmost to true
click menu item "Close Tab" of menu "File" of menu bar 1
end tell
end tell
end if
end tell
]])
end)
hs.window.filter.new('Safari')
:subscribe(hs.window.filter.windowFocused,function() close_app:enable() end)
:subscribe(hs.window.filter.windowUnfocused,function() close_app:disable() end)
-- Function to create a hotkey with state tracking
local managedHotkeys = {}
function createManagedHotkey(mods, key, pressFn)
local hk = hs.hotkey.new(mods, key, pressFn)
table.insert(managedHotkeys, {hotkey = hk, enabled = false}) -- Initially disabled
return hk
end
-- Define your hotkeys using the new function
createManagedHotkey({ "cmd" }, "E", open("Finder")):enable()
createManagedHotkey({ "cmd" }, "Y", open("Youtube")):enable()
createManagedHotkey({ "cmd" }, "T", open("Google Translate")):enable()
createManagedHotkey({ "cmd","⌥" }, "D", open("Google Docs")):enable()
createManagedHotkey({ "cmd","⌥" }, "S", open("Google Sheets")):enable()
createManagedHotkey({ "cmd" }, "D", open("Google Drive")):enable()
createManagedHotkey({ "cmd" }, "1", open_url(URL)):enable()
createManagedHotkey({ "cmd" }, "2", open("Gmail Danilo")):enable()
createManagedHotkey({ "cmd" }, "3", open("Microsoft Outlook")):enable()
createManagedHotkey({ "cmd" }, "K", open("Google Keep")):enable()
createManagedHotkey({ "cmd" }, "M", open("Google Maps")):enable()
createManagedHotkey({ "cmd", "option" }, "C", open("Google Calendar")):enable()
createManagedHotkey({ "cmd" }, "W", open("WhatsApp")):enable()
createManagedHotkey({ "cmd"}, "X", open_NewTab("Safari")):enable()
createManagedHotkey({ "cmd" }, "X", open_NewTab("Microsoft Edge")):enable()
-- Toggle function to enable/disable all managed hotkeys
function toggleHotkeys()
local anyEnabled = false
for _, hk in ipairs(managedHotkeys) do
if hk.enabled then
anyEnabled = true
break
end
end
if anyEnabled then
for _, hk in ipairs(managedHotkeys) do
hk.hotkey:disable()
hk.enabled = false
end
-- These calls actually execute the functions returned by closeApp and open
closeApp("Tiles")()
closeApp("BetterTouchTool")()
hs.alert.show("Hotkeys Disabled")
else
for _, hk in ipairs(managedHotkeys) do
hk.hotkey:enable()
hk.enabled = true
end
-- These calls actually execute the functions returned by closeApp and open
open("Tiles")()
open("BetterTouchTool")()
hs.alert.show("Hotkeys Enabled")
end
end
-- Bind the toggle function to a shortcut
hs.hotkey.bind({"cmd", "option", "control"}, "S", toggleHotkeys)