From 87421140157b41f782172da88819982a328b31a3 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 25 Apr 2025 09:12:38 -0700 Subject: [PATCH] Fix warnings in UserApp.swift. --- .../Sources/RSCore/AppKit/UserApp.swift | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/Modules/RSCore/Sources/RSCore/AppKit/UserApp.swift b/Modules/RSCore/Sources/RSCore/AppKit/UserApp.swift index 974f82875..eb79a04d6 100644 --- a/Modules/RSCore/Sources/RSCore/AppKit/UserApp.swift +++ b/Modules/RSCore/Sources/RSCore/AppKit/UserApp.swift @@ -63,7 +63,7 @@ public final class UserApp { path = bundleURL.path } else { - path = NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: bundleID) + path = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleID)?.path } if icon == nil, let path = path { icon = NSWorkspace.shared.icon(forFile: path) @@ -71,7 +71,7 @@ public final class UserApp { return } - path = NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: bundleID) + path = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleID)?.path if let path = path { if icon == nil { icon = NSWorkspace.shared.icon(forFile: path) @@ -84,7 +84,7 @@ public final class UserApp { } } - public func launchIfNeeded() -> Bool { + public func launchIfNeeded() async -> Bool { // Return true if already running. // Return true if not running and successfully gets launched. @@ -99,20 +99,29 @@ public final class UserApp { } let url = URL(fileURLWithPath: path) - if let app = try? NSWorkspace.shared.launchApplication(at: url, options: [.withErrorPresentation], configuration: [:]) { - runningApplication = app - if app.isFinishedLaunching { - return true - } - Thread.sleep(forTimeInterval: 1.0) // Give the app time to launch. This is ugly. - if app.isFinishedLaunching { - return true - } - Thread.sleep(forTimeInterval: 1.0) // Give it some *more* time. - return true - } - return false + do { + let configuration = NSWorkspace.OpenConfiguration() + configuration.promptsUserIfNeeded = true + + let app = try await NSWorkspace.shared.openApplication(at: url, configuration: configuration) + runningApplication = app + + if app.isFinishedLaunching { + return true + } + + try? await Task.sleep(for: .seconds(1)) // Give the app time to launch. This is ugly. + if app.isFinishedLaunching { + return true + } + + try? await Task.sleep(for: .seconds(1)) // Give it some *more* time. + return true + + } catch { + return false + } } public func bringToFront() -> Bool {