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