feat: add enhanced logging for browser URL detection and AppleScript execution
This commit is contained in:
parent
e1d9be4261
commit
5d80c68214
@ -1,13 +1,25 @@
|
||||
tell application "Firefox"
|
||||
activate
|
||||
delay 0.05
|
||||
tell application "System Events"
|
||||
keystroke "l" using command down
|
||||
delay 0.05
|
||||
keystroke "c" using command down
|
||||
delay 0.05
|
||||
keystroke tab
|
||||
try
|
||||
tell application "Firefox"
|
||||
try
|
||||
activate
|
||||
delay 0.1
|
||||
tell application "System Events"
|
||||
try
|
||||
keystroke "l" using command down
|
||||
delay 0.1
|
||||
keystroke "c" using command down
|
||||
delay 0.1
|
||||
keystroke tab
|
||||
on error errMsg
|
||||
return "ERROR: System Events failed: " & errMsg
|
||||
end try
|
||||
end tell
|
||||
delay 0.1
|
||||
return (the clipboard as text)
|
||||
on error errMsg
|
||||
return "ERROR: Firefox activation failed: " & errMsg
|
||||
end try
|
||||
end tell
|
||||
delay 0.05
|
||||
return (the clipboard as text)
|
||||
end tell
|
||||
on error errMsg
|
||||
return "ERROR: Firefox application not available: " & errMsg
|
||||
end try
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import Foundation
|
||||
import AppKit
|
||||
import os
|
||||
|
||||
class ActiveWindowService: ObservableObject {
|
||||
static let shared = ActiveWindowService()
|
||||
@ -7,6 +8,11 @@ class ActiveWindowService: ObservableObject {
|
||||
private var enhancementService: AIEnhancementService?
|
||||
private let browserURLService = BrowserURLService.shared
|
||||
|
||||
private let logger = Logger(
|
||||
subsystem: "com.prakashjoshipax.VoiceInk",
|
||||
category: "browser.detection"
|
||||
)
|
||||
|
||||
private init() {}
|
||||
|
||||
func configure(with enhancementService: AIEnhancementService) {
|
||||
@ -30,26 +36,27 @@ class ActiveWindowService: ObservableObject {
|
||||
|
||||
// Check if the current app is a supported browser
|
||||
if let browserType = BrowserType.allCases.first(where: { $0.bundleIdentifier == bundleIdentifier }) {
|
||||
print("🌐 Detected Browser: \(browserType.displayName)")
|
||||
logger.debug("🌐 Detected Browser: \(browserType.displayName)")
|
||||
|
||||
do {
|
||||
// Try to get the current URL
|
||||
logger.debug("📝 Attempting to get URL from \(browserType.displayName)")
|
||||
let currentURL = try await browserURLService.getCurrentURL(from: browserType)
|
||||
print("📍 Current URL: \(currentURL)")
|
||||
logger.debug("📍 Successfully got URL: \(currentURL)")
|
||||
|
||||
// Check for URL-specific configuration
|
||||
if let (config, urlConfig) = PowerModeManager.shared.getConfigurationForURL(currentURL) {
|
||||
print("⚙️ Found URL Configuration: \(config.appName) - URL: \(urlConfig.url)")
|
||||
logger.debug("⚙️ Found URL Configuration: \(config.appName) - URL: \(urlConfig.url)")
|
||||
// Apply URL-specific configuration
|
||||
var updatedConfig = config
|
||||
updatedConfig.selectedPrompt = urlConfig.promptId
|
||||
await applyConfiguration(updatedConfig)
|
||||
return
|
||||
} else {
|
||||
print("📝 No URL configuration found for: \(currentURL)")
|
||||
logger.debug("📝 No URL configuration found for: \(currentURL)")
|
||||
}
|
||||
} catch {
|
||||
print("❌ Failed to get URL from \(browserType.displayName): \(error)")
|
||||
logger.error("❌ Failed to get URL from \(browserType.displayName): \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import Foundation
|
||||
import AppKit
|
||||
import os
|
||||
|
||||
enum BrowserType {
|
||||
case safari
|
||||
@ -81,13 +82,27 @@ enum BrowserURLError: Error {
|
||||
class BrowserURLService {
|
||||
static let shared = BrowserURLService()
|
||||
|
||||
private let logger = Logger(
|
||||
subsystem: "com.prakashjoshipax.VoiceInk",
|
||||
category: "browser.applescript"
|
||||
)
|
||||
|
||||
private init() {}
|
||||
|
||||
func getCurrentURL(from browser: BrowserType) async throws -> String {
|
||||
guard let scriptURL = Bundle.main.url(forResource: browser.scriptName, withExtension: "scpt") else {
|
||||
logger.error("❌ AppleScript file not found: \(browser.scriptName).scpt")
|
||||
throw BrowserURLError.scriptNotFound
|
||||
}
|
||||
|
||||
logger.debug("🔍 Attempting to execute AppleScript for \(browser.displayName)")
|
||||
|
||||
// Check if browser is running
|
||||
if !isRunning(browser) {
|
||||
logger.error("❌ Browser not running: \(browser.displayName)")
|
||||
throw BrowserURLError.browserNotRunning
|
||||
}
|
||||
|
||||
let task = Process()
|
||||
task.launchPath = "/usr/bin/osascript"
|
||||
task.arguments = [scriptURL.path]
|
||||
@ -97,19 +112,31 @@ class BrowserURLService {
|
||||
task.standardError = pipe
|
||||
|
||||
do {
|
||||
logger.debug("▶️ Executing AppleScript for \(browser.displayName)")
|
||||
try task.run()
|
||||
task.waitUntilExit()
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
if let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) {
|
||||
if output.isEmpty {
|
||||
logger.error("❌ Empty output from AppleScript for \(browser.displayName)")
|
||||
throw BrowserURLError.noActiveTab
|
||||
}
|
||||
|
||||
// Check if output contains error messages
|
||||
if output.lowercased().contains("error") {
|
||||
logger.error("❌ AppleScript error for \(browser.displayName): \(output)")
|
||||
throw BrowserURLError.executionFailed
|
||||
}
|
||||
|
||||
logger.debug("✅ Successfully retrieved URL from \(browser.displayName): \(output)")
|
||||
return output
|
||||
} else {
|
||||
logger.error("❌ Failed to decode output from AppleScript for \(browser.displayName)")
|
||||
throw BrowserURLError.executionFailed
|
||||
}
|
||||
} catch {
|
||||
logger.error("❌ AppleScript execution failed for \(browser.displayName): \(error.localizedDescription)")
|
||||
throw BrowserURLError.executionFailed
|
||||
}
|
||||
}
|
||||
@ -117,6 +144,8 @@ class BrowserURLService {
|
||||
func isRunning(_ browser: BrowserType) -> Bool {
|
||||
let workspace = NSWorkspace.shared
|
||||
let runningApps = workspace.runningApplications
|
||||
return runningApps.contains { $0.bundleIdentifier == browser.bundleIdentifier }
|
||||
let isRunning = runningApps.contains { $0.bundleIdentifier == browser.bundleIdentifier }
|
||||
logger.debug("\(browser.displayName) running status: \(isRunning)")
|
||||
return isRunning
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user