Repository navigation
Expand file tree
/
Copy pathinit.lua
More file actions
220 lines (208 loc) · 8.15 KB
/
Copy pathinit.lua
File metadata and controls
220 lines (208 loc) · 8.15 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
---------------------------------------------------------------------------------------------------
-- TIMEOUT OBJECT DEFINITION
---------------------------------------------------------------------------------------------------
local Timeout = {
startTime = os.time(),
timeoutInSeconds = 0
}
function Timeout:reached()
return (hs.timer.seconds(os.time()) - hs.timer.seconds(self.startTime)) > self.timeoutInSeconds
end
function Timeout:new(t)
t = t or {}
setmetatable(t, self)
self.__index = self
return t
end
---------------------------------------------------------------------------------------------------
-- PACKAGE VARIABLE
---------------------------------------------------------------------------------------------------
local doCheck=true
local isCheckRunning=false
local doShowInfo=false
local lookuptable = {
[hs.application.watcher.activated] = "activated",
[hs.application.watcher.deactivated] = "deactivated",
[hs.application.watcher.hidden] = "hidden",
[hs.application.watcher.launched] = "launched",
[hs.application.watcher.launching] = "launching",
[hs.application.watcher.terminated] = "terminated",
[hs.application.watcher.unhidden] = "unhidden"
}
---------------------------------------------------------------------------------------------------
-- RUN FUNCTION ON GIVEN APPS
---------------------------------------------------------------------------------------------------
local function onRunningApps(app, eventType, apps, fn)
if not doCheck or isCheckRunning then
doCheck=true
return
end
isCheckRunning=true
if eventType ~= hs.application.watcher.launching
and eventType ~= hs.application.watcher.activated then
isCheckRunning=false
return
end
local runningApps={}
for i, a in ipairs(apps) do
local runningApp = hs.application.find(a)
if runningApp then
table.insert(runningApps, runningApp)
end
end
if #runningApps > 0 then
fn(app, runningApps)
else
isCheckRunning=false
end
end
---------------------------------------------------------------------------------------------------
-- OBJECT APP.NAME TO STRING WITH SEPARATOR
---------------------------------------------------------------------------------------------------
local function appsToString(apps, separator)
local appNames
for i, app in ipairs(apps) do
if appNames then
appNames = appNames .. ";" .. app:name()
else
appNames = app:name()
end
end
return string.gsub(appNames, "%;", separator)
end
---------------------------------------------------------------------------------------------------
-- RETURNS TABLE WITH RUNNING APPS
---------------------------------------------------------------------------------------------------
local function getRunningApps(apps)
apps = apps or {}
local runningApps = {}
if #apps == 0 then
return apps
end
for i, a in ipairs(apps) do
if a:isRunning() then
table.insert(runningApps, a)
end
end
return runningApps
end
---------------------------------------------------------------------------------------------------
-- ACTIVATES FIRST APP FROM TABLE
---------------------------------------------------------------------------------------------------
local function activateFirstApp(apps)
apps = apps or {}
for i, app in ipairs(apps) do
if app:isRunning() then
doCheck=false
app:activate()
break
end
end
end
---------------------------------------------------------------------------------------------------
-- CLOSES APPS
---------------------------------------------------------------------------------------------------
local function closeApps(mainApp, apps)
if #apps == 0 then
isCheckRunning=false
return
end
local runningApps = apps
local appNames = appsToString(apps, "\n")
local button=hs.dialog.blockAlert("Application(s) are running", appNames .."\n\nTo exit applications press 'Ok'.", "Ok", "Cancel", "NSCriticalAlertStyle")
if button == "Ok" then
for i, app in ipairs(apps) do
app:kill()
end
local timeout = Timeout:new({startTime=os.time(), timeoutInSeconds=10})
local function predicate()
isCheckRunning=true
runningApps = getRunningApps(runningApps)
activateFirstApp(runningApps)
return #runningApps == 0 or timeout:reached()
end
local function action()
if #runningApps == 0 then
hs.alert.show("All applications closed")
else
appNames = appsToString(runningApps, "\n")
hs.dialog.blockAlert("Application(s) are still running", appNames)
activateFirstApp(runningApps)
end
isCheckRunning=false
end
hs.timer.waitUntil(predicate, action)
else
doCheck=false
mainApp:activate()
isCheckRunning=false
end
end
---------------------------------------------------------------------------------------------------
-- SHOWS WARNING: APPS ARE RUNNING
---------------------------------------------------------------------------------------------------
local function showWarning(mainApp, apps)
if #apps == 0 then
isCheckRunning=false
return
end
local appNames = appsToString(apps, "\n")
hs.dialog.blockAlert("Application(s) are running", appNames)
doCheck=false
mainApp:activate()
isCheckRunning=false
end
---------------------------------------------------------------------------------------------------
-- APPLICATION WATCHER
---------------------------------------------------------------------------------------------------
local function applicationWatcher(appName, eventType, app)
if doShowInfo then
hs.alert.show("appName: ".. appName.. "\neventType: ".. lookuptable[eventType] .. "\nbundleID: ".. app:bundleID())
print("\nappName: ", appName, "\neventType: ", lookuptable[eventType], "\nbundleID: ", app:bundleID())
end
-- com.citrix.receiver.nomas
-- com.citrix.receiver.icaviewer.mac
if appName == "Citrix Viewer" then
onRunningApps(app, eventType, {"ch.protonvpn.mac","com.strongvpn.StrongVPN2-Client"}, closeApps)
elseif appName == "Citrix Workspace" then
onRunningApps(app, eventType, {"ch.protonvpn.mac","com.strongvpn.StrongVPN2-Client"}, closeApps)
elseif appName == "StrongVPN" then
onRunningApps(app, eventType, {"com.citrix.receiver.icaviewer.mac"}, showWarning)
elseif appName == "ProtonVPN" then
onRunningApps(app, eventType, {"com.citrix.receiver.icaviewer.mac"}, showWarning)
end
end
_G.appWatcher = hs.application.watcher.new(applicationWatcher):start()
---------------------------------------------------------------------------------------------------
-- RELOAD CONFIG
---------------------------------------------------------------------------------------------------
local function reloadConfig(files)
local 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
_G.configWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
---------------------------------------------------------------------------------------------------
-- KEY BINDING
---------------------------------------------------------------------------------------------------
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "w", function()
hs.alert.show(
"doCheck: " .. tostring(doCheck) ..
"\nisCheckRunning: ".. tostring(isCheckRunning) ..
"\ndoShowInfo: ".. tostring(doShowInfo)
)
end)
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "l", function ()
doShowInfo = not doShowInfo
hs.alert.show("doShowInfo: ".. tostring(doShowInfo))
end)
---------------------------------------------------------------------------------------------------
-- CONFIG LOADED
---------------------------------------------------------------------------------------------------
hs.alert.show("Config loaded")