Fix deprecation warnings.

This commit is contained in:
Brent Simmons 2024-11-06 21:35:14 -08:00
parent 8cf388e55c
commit f63e0628ea
3 changed files with 54 additions and 36 deletions

View File

@ -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? {

View File

@ -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)
}
}
}

View File

@ -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)
}
}