Skip to content

Commit 1370d29

Browse files
authored
[macos] Improve handling of missing application attributes (#105)
* fix: avoid unwrapping values that are likely to be null and cause a crash * Don't fail if there is no bundle identifier * Improve logging
1 parent 61b482b commit 1370d29

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

aw_watcher_window/macos.swift

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func logPrefix(_ level: String) -> String {
101101
let logLevel = ProcessInfo.processInfo.environment["LOG_LEVEL"]?.uppercased() ?? "INFO"
102102

103103
func debug(_ msg: String) {
104-
if(logLevel == "DEBUG") {
104+
if (logLevel == "DEBUG") {
105105
print("\(logPrefix("DEBUG")) \(msg)")
106106
fflush(stdout)
107107
}
@@ -313,26 +313,33 @@ class MainThing {
313313
return
314314
}
315315

316-
guard let bundleIdentifier = frontmost.bundleIdentifier else {
317-
log("Failed to get bundle identifier from frontmost application")
318-
return
319-
}
320-
321316
// calculate now before executing any scripting since that can take some time
322317
let nowTime = Date.now
323318

324319
var windowTitle: AnyObject?
325320
AXUIElementCopyAttributeValue(axElement, kAXTitleAttribute as CFString, &windowTitle)
326321

327-
var data = NetworkMessage(app: frontmost.localizedName!, title: windowTitle as? String ?? "")
322+
let applicationName = frontmost.localizedName ?? frontmost.bundleIdentifier ?? ""
323+
var data = NetworkMessage(app: applicationName, title: windowTitle as? String ?? "")
328324

329-
if CHROME_BROWSERS.contains(frontmost.localizedName!) {
325+
if CHROME_BROWSERS.contains(applicationName) {
330326
debug("Chrome browser detected, extracting URL and title")
331327

328+
guard let bundleIdentifier = frontmost.bundleIdentifier else {
329+
log("Failed to get bundle identifier from frontmost application, which was recognized to be Chrome")
330+
return
331+
}
332332
let chromeObject: ChromeProtocol = SBApplication.init(bundleIdentifier: bundleIdentifier)!
333333

334-
let frontWindow = chromeObject.windows!()[0]
335-
let activeTab = frontWindow.activeTab!
334+
guard let windows = chromeObject.windows,
335+
let frontWindow = windows().first else {
336+
log("Failed to get chrome front window")
337+
return
338+
}
339+
guard let activeTab = frontWindow.activeTab else {
340+
log("Failed to get chrome active tab")
341+
return
342+
}
336343

337344
if frontWindow.mode == "incognito" {
338345
data = NetworkMessage(app: "", title: "")
@@ -345,26 +352,37 @@ class MainThing {
345352

346353
if let tabTitle = activeTab.title {
347354
if(tabTitle != "" && data.title != tabTitle) {
348-
error("tab title diff: \(tabTitle), window title: \(data.title ?? "")")
355+
error("tab title diff: \(tabTitle), window title: \(data.title)")
349356
data.title = tabTitle
350357
}
351358
}
352359
}
353360
} else if frontmost.localizedName == "Safari" {
354361
debug("Safari browser detected, extracting URL and title")
355362

363+
guard let bundleIdentifier = frontmost.bundleIdentifier else {
364+
log("Failed to get bundle identifier from frontmost application, which was recognized to be Safari")
365+
return
366+
}
356367
let safariObject: SafariApplication = SBApplication.init(bundleIdentifier: bundleIdentifier)!
357368

358-
let frontWindow = safariObject.windows!()[0]
359-
let activeTab = frontWindow.currentTab!
369+
guard let windows = safariObject.windows,
370+
let frontWindow = windows().first else {
371+
log("Failed to get safari front window")
372+
return
373+
}
374+
guard let activeTab = frontWindow.currentTab else {
375+
log("Failed to get safari active tab")
376+
return
377+
}
360378

361379
// Safari doesn't allow incognito mode to be inspected, so we do not know if we should hide the url
362380
data.url = activeTab.URL
363381

364382
// comment above applies here as well
365383
if let tabTitle = activeTab.name {
366384
if tabTitle != "" && data.title != tabTitle {
367-
error("tab title diff: \(tabTitle), window title: \(data.title ?? "")")
385+
error("tab title diff: \(tabTitle), window title: \(data.title)")
368386
data.title = tabTitle
369387
}
370388
}
@@ -378,8 +396,7 @@ class MainThing {
378396
debug("Focused window changed")
379397

380398
if oldWindow != nil {
381-
AXObserverRemoveNotification(
382-
observer, oldWindow!, kAXFocusedWindowChangedNotification as CFString)
399+
AXObserverRemoveNotification(observer, oldWindow!, kAXFocusedWindowChangedNotification as CFString)
383400
}
384401

385402
let selfPtr = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
@@ -436,8 +453,7 @@ class MainThing {
436453
}, &observer)
437454

438455
let selfPtr = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
439-
AXObserverAddNotification(
440-
observer!, focusedApp, kAXFocusedWindowChangedNotification as CFString, selfPtr)
456+
AXObserverAddNotification(observer!, focusedApp, kAXFocusedWindowChangedNotification as CFString, selfPtr)
441457

442458
CFRunLoopAddSource(
443459
RunLoop.current.getCFRunLoop(),

0 commit comments

Comments
 (0)