@@ -35,13 +35,25 @@ final class SafariPlugin: AppPlugin {
3535 func performInput( action: AppAction , context: AppContext ) async throws -> String { " " }
3636
3737 func performOutput( action: AppAction , context: AppContext ) async throws {
38- // Heuristic: find first URL in the context and open; otherwise open Safari
38+ // Heuristic: find first URL; open in background if configured
39+ let openInBackground = ( context. settings [ " open_background " ] ?? " false " ) . lowercased ( ) == " true "
3940 if let url = firstURL ( in: context. conversationText) {
40- if !NSWorkspace. shared. open ( url) {
41- NSWorkspace . shared. launchApplication ( withBundleIdentifier: " com.apple.Safari " , options: [ . default] , additionalEventParamDescriptor: nil , launchIdentifier: nil )
41+ if openInBackground {
42+ let esc = url. absoluteString. replacingOccurrences ( of: " \\ " , with: " \\ \\ " ) . replacingOccurrences ( of: " \" " , with: " \\ \" " )
43+ let script = " tell application \" Safari \" to make new document with properties {URL: \" \( esc) \" } "
44+ var err : NSDictionary ?
45+ _ = NSAppleScript ( source: script) ? . executeAndReturnError ( & err)
46+ } else {
47+ _ = NSWorkspace . shared. open ( url)
4248 }
4349 } else {
44- NSWorkspace . shared. launchApplication ( withBundleIdentifier: " com.apple.Safari " , options: [ . default] , additionalEventParamDescriptor: nil , launchIdentifier: nil )
50+ if let appURL = NSWorkspace . shared. urlForApplication ( withBundleIdentifier: " com.apple.Safari " ) {
51+ try ? NSWorkspace . shared. openApplication ( at: appURL, configuration: . init( ) ,
52+ completionHandler: nil )
53+ } else {
54+ var err : NSDictionary ?
55+ _ = NSAppleScript ( source: " tell application \" Safari \" to activate " ) ? . executeAndReturnError ( & err)
56+ }
4557 }
4658 }
4759
@@ -58,7 +70,18 @@ final class SafariPlugin: AppPlugin {
5870 }
5971
6072 func statusBadges( ) -> [ PluginStatusBadge ] {
61- [ PluginStatusBadge ( " Automation: Unknown " , kind: . info) , PluginStatusBadge ( " Custom Settings " , kind: . success) ]
73+ var err : NSDictionary ?
74+ NSAppleScript ( source: " tell application \" Safari \" to get name " ) ? . executeAndReturnError ( & err)
75+ if let n = err ? [ NSAppleScript . errorNumber] as? Int , n == - 1743 {
76+ return [ PluginStatusBadge ( " Automation: Denied " , kind: . error) , PluginStatusBadge ( " Custom Settings " , kind: . success) ]
77+ }
78+ if err == nil { return [ PluginStatusBadge ( " Automation: Granted " , kind: . success) , PluginStatusBadge ( " Custom Settings " , kind: . success) ] }
79+ return [ PluginStatusBadge ( " Automation: Unknown " , kind: . info) , PluginStatusBadge ( " Custom Settings " , kind: . success) ]
80+ }
81+
82+ func debugPreview( action: AppAction , context: AppContext ) -> String ? {
83+ if let url = firstURL ( in: context. conversationText) { return " Open URL: \( url. absoluteString) " }
84+ return " Open Safari "
6285 }
6386}
6487
0 commit comments