1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import Foundation
#if canImport(AppKit)
import AppKit
#endif
/// Launches macOS applications safely using native APIs.
public final class ApplicationLauncher: Sendable {
public init() {}
public func launch(appPath: String, arguments: [String] = []) async throws -> ExecutionResult {
let startTime = Date()
let validatedPath = try PathValidator.validateApplication(at: appPath).get()
let appURL = URL(fileURLWithPath: validatedPath)
#if canImport(AppKit)
let config = NSWorkspace.OpenConfiguration()
if !arguments.isEmpty {
config.arguments = arguments
}
do {
_ = try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<NSRunningApplication, Error>) in
NSWorkspace.shared.openApplication(at: appURL, configuration: config) { app, error in
if let error = error {
continuation.resume(throwing: error)
} else if let app = app {
continuation.resume(returning: app)
} else {
continuation.resume(throwing: ExecutionError.applicationLaunchFailed("Application launch failed without error info."))
}
}
}
let duration = Date().timeIntervalSince(startTime)
return ExecutionResult(
exitCode: 0,
stdout: "Successfully launched application: \(appURL.lastPathComponent)",
stderr: "",
duration: duration
)
} catch {
// If native NSWorkspace openApplication threw, try /usr/bin/open as fallback
let runner = ProcessRunner()
var args = ["-a", validatedPath]
if !arguments.isEmpty {
args.append("--args")
args.append(contentsOf: arguments)
}
let res = try await runner.run(
executableURL: URL(fileURLWithPath: "/usr/bin/open"),
arguments: args
)
return ExecutionResult(
exitCode: res.exitCode,
stdout: "Launched via open tool: \(appURL.lastPathComponent)",
stderr: res.stderr,
duration: Date().timeIntervalSince(startTime)
)
}
#else
let runner = ProcessRunner()
var args = ["-a", validatedPath]
if !arguments.isEmpty {
args.append("--args")
args.append(contentsOf: arguments)
}
let res = try await runner.run(
executableURL: URL(fileURLWithPath: "/usr/bin/open"),
arguments: args
)
return ExecutionResult(
exitCode: res.exitCode,
stdout: "Launched via open tool: \(appURL.lastPathComponent)",
stderr: res.stderr,
duration: Date().timeIntervalSince(startTime)
)
#endif
}
}
|