Skip to content
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
45 changes: 42 additions & 3 deletions aw_watcher_window/macos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ class MainThing {
"Brave Browser",
]

let FIREFOX_BROWSERS = [
"Firefox",
"Firefox Developer Edition",
"Firefox Nightly",
]

@objc func pollActiveWindow() {
debug("Polling active window")

Expand Down Expand Up @@ -327,7 +333,7 @@ class MainThing {
var data = NetworkMessage(app: frontmost.localizedName!, title: windowTitle as? String ?? "")

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

let chromeObject: ChromeProtocol = SBApplication.init(bundleIdentifier: bundleIdentifier)!

Expand All @@ -345,11 +351,44 @@ class MainThing {

if let tabTitle = activeTab.title {
if(tabTitle != "" && data.title != tabTitle) {
error("tab title diff: \(tabTitle), window title: \(data.title ?? "")")
debug("tab title diff: \(tabTitle), window title: \(data.title)")
data.title = tabTitle
}
}
}
} else if FIREFOX_BROWSERS.contains(frontmost.localizedName!) {
debug("Firefox-based browser detected, extracting URL using AppleScript")

// WARNING: This AppleScript relies on Firefox's specific UI hierarchy:
// window 1 -> group 1 -> toolbar "Navigation" -> combo box 1 (URL bar)
// This is fragile and may break if Firefox changes its UI structure
func getFirefoxURL(_ browserName: String) -> String? {
let script = """
tell application "System Events"
tell process "\(browserName)"
if exists (window 1) then
if exists (toolbar "Navigation" of group 1 of window 1) then
return value of combo box 1 of group 1 of toolbar "Navigation" of group 1 of window 1
end if
end if
end tell
end tell
"""

let appleScript = NSAppleScript(source: script)
var scriptError: NSDictionary?
if let result = appleScript?.executeAndReturnError(&scriptError).stringValue {
return result
} else if let scriptError = scriptError {
error("Failed to get Firefox URL for \(browserName): \(scriptError)")
return nil
}
return nil
}

if let url = getFirefoxURL(frontmost.localizedName!) {
data.url = url
}
} else if frontmost.localizedName == "Safari" {
debug("Safari browser detected, extracting URL and title")

Expand All @@ -364,7 +403,7 @@ class MainThing {
// 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 ?? "")")
debug("tab title diff: \(tabTitle), window title: \(data.title)")
data.title = tabTitle
}
}
Expand Down