Skip to content

Commit 82d9673

Browse files
authored
fix(macOS): guards against missing notification data (#86)
* Flush logs to avoid logging delays * guards against missing frontmost application this was causing the app to crash * Log when tab title diff is encountered * guard on bundle identifier
1 parent f08afe8 commit 82d9673

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

aw_watcher_window/macos.swift

+25-6
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ let logLevel = ProcessInfo.processInfo.environment["LOG_LEVEL"]?.uppercased() ??
103103
func debug(_ msg: String) {
104104
if(logLevel == "DEBUG") {
105105
print("\(logPrefix("DEBUG")) \(msg)")
106+
fflush(stdout)
106107
}
107108
}
108109

109110
func log(_ msg: String) {
110111
print("\(logPrefix("INFO")) \(msg)")
112+
fflush(stdout)
111113
}
112114

113115
func error(_ msg: String) {
114116
print("\(logPrefix("ERROR")) \(msg)")
117+
fflush(stdout)
115118
}
116119

117120
// Placeholder values, set in start() from CLI arguments
@@ -278,8 +281,15 @@ class MainThing {
278281
axElement: AXUIElement,
279282
notification: CFString
280283
) {
281-
let frontmost = NSWorkspace.shared.frontmostApplication!
282-
let bundleIdentifier = frontmost.bundleIdentifier!
284+
guard let frontmost = NSWorkspace.shared.frontmostApplication else {
285+
log("Failed to get frontmost application from window title notification")
286+
return
287+
}
288+
289+
guard let bundleIdentifier = frontmost.bundleIdentifier else {
290+
log("Failed to get bundle identifier from frontmost application")
291+
return
292+
}
283293

284294
// calculate now before executing any scripting since that can take some time
285295
let nowTime = Date.now
@@ -307,7 +317,10 @@ class MainThing {
307317
// the title properly and will return a blank string
308318

309319
if let tabTitle = activeTab.title {
310-
if(tabTitle != "") { data.title = tabTitle }
320+
if(tabTitle != "" && data.title != tabTitle) {
321+
error("tab title diff: \(tabTitle), window title: \(data.title ?? "")")
322+
data.title = tabTitle
323+
}
311324
}
312325
}
313326
} else if frontmost.localizedName == "Safari" {
@@ -323,9 +336,11 @@ class MainThing {
323336

324337
// comment above applies here as well
325338
if let tabTitle = activeTab.name {
326-
if tabTitle != "" { data.title = tabTitle }
339+
if tabTitle != "" && data.title != tabTitle {
340+
error("tab title diff: \(tabTitle), window title: \(data.title ?? "")")
341+
data.title = tabTitle
342+
}
327343
}
328-
329344
}
330345

331346
let heartbeat = Heartbeat(timestamp: nowTime, data: data)
@@ -360,7 +375,11 @@ class MainThing {
360375
)
361376
}
362377

363-
let frontmost = NSWorkspace.shared.frontmostApplication!
378+
guard let frontmost = NSWorkspace.shared.frontmostApplication else {
379+
log("Failed to get frontmost application from app change notification")
380+
return
381+
}
382+
364383
let pid = frontmost.processIdentifier
365384
let focusedApp = AXUIElementCreateApplication(pid)
366385

0 commit comments

Comments
 (0)