From f63e0628ea3360ad73560698ec3aff1f5dc83b14 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 6 Nov 2024 21:35:14 -0800 Subject: [PATCH] Fix deprecation warnings. --- RSCore/Sources/RSCore/AppKit/UserApp.swift | 43 +++++++++++-------- .../SendToMarsEditCommand.swift | 10 ++++- .../SendToMicroBlogCommand.swift | 37 ++++++++-------- 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/RSCore/Sources/RSCore/AppKit/UserApp.swift b/RSCore/Sources/RSCore/AppKit/UserApp.swift index 974f82875..87ea16ddd 100644 --- a/RSCore/Sources/RSCore/AppKit/UserApp.swift +++ b/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 { @@ -121,7 +130,7 @@ public final class UserApp { // Does not automatically launch the app first. updateStatus() - return runningApplication?.activate(options: [.activateIgnoringOtherApps]) ?? false + return runningApplication?.activate() ?? false } public func targetDescriptor() -> NSAppleEventDescriptor? { diff --git a/Shared/ExtensionPoints/SendToMarsEditCommand.swift b/Shared/ExtensionPoints/SendToMarsEditCommand.swift index 89a867fff..f2207af64 100644 --- a/Shared/ExtensionPoints/SendToMarsEditCommand.swift +++ b/Shared/ExtensionPoints/SendToMarsEditCommand.swift @@ -29,11 +29,17 @@ final class SendToMarsEditCommand: SendToCommand { guard let article = (object as? ArticlePasteboardWriter)?.article else { return } - guard let app = appToUse(), app.launchIfNeeded(), app.bringToFront() else { + guard let app = appToUse() else { return } - send(article, to: app) + Task { + guard await app.launchIfNeeded(), app.bringToFront() else { + return + } + + send(article, to: app) + } } } diff --git a/Shared/ExtensionPoints/SendToMicroBlogCommand.swift b/Shared/ExtensionPoints/SendToMicroBlogCommand.swift index f458138d8..342bec319 100644 --- a/Shared/ExtensionPoints/SendToMicroBlogCommand.swift +++ b/Shared/ExtensionPoints/SendToMicroBlogCommand.swift @@ -37,24 +37,27 @@ final class SendToMicroBlogCommand: SendToCommand { guard let article = (object as? ArticlePasteboardWriter)?.article else { return } - guard microBlogApp.launchIfNeeded(), microBlogApp.bringToFront() else { - return + + Task { + guard await microBlogApp.launchIfNeeded(), microBlogApp.bringToFront() else { + return + } + + // TODO: get text from contentHTML or contentText if no title and no selectedText. + // TODO: consider selectedText. + + let s = article.attributionString + article.linkString + + let urlQueryDictionary = ["text": s] + guard let urlQueryString = urlQueryDictionary.urlQueryString else { + return + } + guard let url = URL(string: "microblog://post?" + urlQueryString) else { + return + } + + NSWorkspace.shared.open(url) } - - // TODO: get text from contentHTML or contentText if no title and no selectedText. - // TODO: consider selectedText. - - let s = article.attributionString + article.linkString - - let urlQueryDictionary = ["text": s] - guard let urlQueryString = urlQueryDictionary.urlQueryString else { - return - } - guard let url = URL(string: "microblog://post?" + urlQueryString) else { - return - } - - NSWorkspace.shared.open(url) } }