NetNewsWire/Shared/ExtensionPoints/SendToMarsEditCommand.swift

85 lines
2.0 KiB
Swift
Raw Normal View History

//
// SendToMarsEditCommand.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
//
// Created by Brent Simmons on 1/8/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
import Articles
import Core
2024-05-04 07:00:35 +02:00
@preconcurrency import AppKitExtras
2024-05-03 20:57:20 +02:00
@MainActor final class SendToMarsEditCommand: SendToCommand {
2023-06-26 01:48:37 +02:00
let title = "MarsEdit"
let image: RSImage? = AppAssets.marsEditIcon
private let marsEditApps = [UserApp(bundleID: "com.red-sweater.marsedit4"), UserApp(bundleID: "com.red-sweater.marsedit")]
func canSendObject(_ object: Any?, selectedText: String?) -> Bool {
2023-06-26 01:48:37 +02:00
appToUse() != nil
}
2024-03-20 07:05:30 +01:00
@MainActor func sendObject(_ object: Any?, selectedText: String?) {
guard canSendObject(object, selectedText: selectedText) else {
return
}
guard let article = (object as? ArticlePasteboardWriter)?.article else {
return
}
guard let app = appToUse() else {
return
}
Task { @MainActor in
guard await app.launchIfNeeded(), app.bringToFront() else {
return
}
send(article, to: app)
}
}
}
2024-05-03 20:57:20 +02:00
@MainActor private extension SendToMarsEditCommand {
2024-05-03 20:57:20 +02:00
func send(_ article: Article, to app: UserApp) {
// App has already been launched.
guard let targetDescriptor = app.targetDescriptor() else {
return
}
let body = article.contentHTML ?? article.contentText ?? article.summary
let authorName = article.authors?.first?.name
let sender = SendToBlogEditorApp(targetDescriptor: targetDescriptor, title: article.title, body: body, summary: article.summary, link: article.externalLink, permalink: article.link, subject: nil, creator: authorName, commentsURL: nil, guid: article.uniqueID, sourceName: article.feed?.nameForDisplay, sourceHomeURL: article.feed?.homePageURL, sourceFeedURL: article.feed?.url)
let _ = sender.send()
}
func appToUse() -> UserApp? {
2024-04-08 02:06:39 +02:00
for app in marsEditApps {
app.updateStatus()
}
for app in marsEditApps {
if app.isRunning {
return app
}
}
for app in marsEditApps {
if app.existsOnDisk {
return app
}
}
return nil
}
}