Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macos] Improve handling of missing application attributes #105

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions aw_watcher_window/macos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func logPrefix(_ level: String) -> String {
let logLevel = ProcessInfo.processInfo.environment["LOG_LEVEL"]?.uppercased() ?? "INFO"

func debug(_ msg: String) {
if(logLevel == "DEBUG") {
if (logLevel == "DEBUG") {
print("\(logPrefix("DEBUG")) \(msg)")
fflush(stdout)
}
Expand Down Expand Up @@ -313,26 +313,33 @@ class MainThing {
return
}

guard let bundleIdentifier = frontmost.bundleIdentifier else {
log("Failed to get bundle identifier from frontmost application")
return
}

// calculate now before executing any scripting since that can take some time
let nowTime = Date.now

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

var data = NetworkMessage(app: frontmost.localizedName!, title: windowTitle as? String ?? "")
let applicationName = frontmost.localizedName ?? frontmost.bundleIdentifier ?? ""
var data = NetworkMessage(app: applicationName, title: windowTitle as? String ?? "")

if CHROME_BROWSERS.contains(frontmost.localizedName!) {
if CHROME_BROWSERS.contains(applicationName) {
debug("Chrome browser detected, extracting URL and title")

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

let frontWindow = chromeObject.windows!()[0]
let activeTab = frontWindow.activeTab!
guard let windows = chromeObject.windows,
let frontWindow = windows().first else {
log("Failed to get chrome front window")
return
}
guard let activeTab = frontWindow.activeTab else {
log("Failed to get chrome active tab")
return
}

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

if let tabTitle = activeTab.title {
if(tabTitle != "" && data.title != tabTitle) {
error("tab title diff: \(tabTitle), window title: \(data.title ?? "")")
error("tab title diff: \(tabTitle), window title: \(data.title)")
data.title = tabTitle
}
}
}
} else if frontmost.localizedName == "Safari" {
debug("Safari browser detected, extracting URL and title")

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

let frontWindow = safariObject.windows!()[0]
let activeTab = frontWindow.currentTab!
guard let windows = safariObject.windows,
let frontWindow = windows().first else {
log("Failed to get safari front window")
return
}
guard let activeTab = frontWindow.currentTab else {
log("Failed to get safari active tab")
return
}

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

// comment above applies here as well
if let tabTitle = activeTab.name {
if tabTitle != "" && data.title != tabTitle {
error("tab title diff: \(tabTitle), window title: \(data.title ?? "")")
error("tab title diff: \(tabTitle), window title: \(data.title)")
data.title = tabTitle
}
}
Expand All @@ -378,8 +396,7 @@ class MainThing {
debug("Focused window changed")

if oldWindow != nil {
AXObserverRemoveNotification(
observer, oldWindow!, kAXFocusedWindowChangedNotification as CFString)
AXObserverRemoveNotification(observer, oldWindow!, kAXFocusedWindowChangedNotification as CFString)
}

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

let selfPtr = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
AXObserverAddNotification(
observer!, focusedApp, kAXFocusedWindowChangedNotification as CFString, selfPtr)
AXObserverAddNotification(observer!, focusedApp, kAXFocusedWindowChangedNotification as CFString, selfPtr)

CFRunLoopAddSource(
RunLoop.current.getCFRunLoop(),
Expand Down