@@ -103,15 +103,18 @@ let logLevel = ProcessInfo.processInfo.environment["LOG_LEVEL"]?.uppercased() ??
103
103
func debug( _ msg: String ) {
104
104
if ( logLevel == " DEBUG " ) {
105
105
print ( " \( logPrefix ( " DEBUG " ) ) \( msg) " )
106
+ fflush ( stdout)
106
107
}
107
108
}
108
109
109
110
func log( _ msg: String ) {
110
111
print ( " \( logPrefix ( " INFO " ) ) \( msg) " )
112
+ fflush ( stdout)
111
113
}
112
114
113
115
func error( _ msg: String ) {
114
116
print ( " \( logPrefix ( " ERROR " ) ) \( msg) " )
117
+ fflush ( stdout)
115
118
}
116
119
117
120
// Placeholder values, set in start() from CLI arguments
@@ -278,8 +281,15 @@ class MainThing {
278
281
axElement: AXUIElement ,
279
282
notification: CFString
280
283
) {
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
+ }
283
293
284
294
// calculate now before executing any scripting since that can take some time
285
295
let nowTime = Date . now
@@ -307,7 +317,10 @@ class MainThing {
307
317
// the title properly and will return a blank string
308
318
309
319
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
+ }
311
324
}
312
325
}
313
326
} else if frontmost. localizedName == " Safari " {
@@ -323,9 +336,11 @@ class MainThing {
323
336
324
337
// comment above applies here as well
325
338
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
+ }
327
343
}
328
-
329
344
}
330
345
331
346
let heartbeat = Heartbeat ( timestamp: nowTime, data: data)
@@ -360,7 +375,11 @@ class MainThing {
360
375
)
361
376
}
362
377
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
+
364
383
let pid = frontmost. processIdentifier
365
384
let focusedApp = AXUIElementCreateApplication ( pid)
366
385
0 commit comments