Make further progress on sending to MarsEdit and Micro.blog.
This commit is contained in:
parent
ccc699741d
commit
f324e65f16
|
@ -8,10 +8,12 @@
|
|||
|
||||
import Cocoa
|
||||
|
||||
// Unlike UndoableCommand commands, you instantiate one of each of these and reuse them.
|
||||
|
||||
protocol SendToCommand {
|
||||
|
||||
func canSendObject(_ object: Any?) -> Bool
|
||||
func sendObject(_ object: Any?)
|
||||
func canSendObject(_ object: Any?, selectedText: String?) -> Bool
|
||||
func sendObject(_ object: Any?, selectedText: String?)
|
||||
}
|
||||
|
||||
extension SendToCommand {
|
||||
|
|
|
@ -10,12 +10,12 @@ import Foundation
|
|||
|
||||
final class SendToMarsEditCommand: SendToCommand {
|
||||
|
||||
func canSendObject(_ object: Any?) -> Bool {
|
||||
func canSendObject(_ object: Any?, selectedText: String?) -> Bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func sendObject(_ object: Any?) {
|
||||
func sendObject(_ object: Any?, selectedText: String?) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
//
|
||||
|
||||
import Cocoa
|
||||
import Data
|
||||
|
||||
// Not undoable.
|
||||
|
||||
|
@ -21,16 +22,53 @@ final class SendToMicroBlogCommand: SendToCommand {
|
|||
NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive(_:)), name: NSApplication.didBecomeActiveNotification, object: nil)
|
||||
}
|
||||
|
||||
func canSendObject(_ object: Any?) -> Bool {
|
||||
func canSendObject(_ object: Any?, selectedText: String?) -> Bool {
|
||||
|
||||
if !appExists {
|
||||
guard appExists else {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
guard let article = object as? Article else {
|
||||
return false
|
||||
}
|
||||
guard let _ = article.preferredLink else {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func sendObject(_ object: Any?) {
|
||||
func sendObject(_ object: Any?, selectedText: String?) {
|
||||
|
||||
guard canSendObject(object, selectedText: selectedText) else {
|
||||
return
|
||||
}
|
||||
guard let article = object as? Article else {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: get text from contentHTML or contentText if no title and no selectedText.
|
||||
var s = ""
|
||||
if let selectedText = selectedText {
|
||||
s += selectedText
|
||||
if let link = article.preferredLink {
|
||||
s += "\n\n\(link)"
|
||||
}
|
||||
}
|
||||
else if let title = article.title {
|
||||
s += title
|
||||
if let link = article.preferredLink {
|
||||
s = "[" + s + "](" + link + ")"
|
||||
}
|
||||
}
|
||||
|
||||
guard let encodedString = s.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
|
||||
return
|
||||
}
|
||||
guard let url = URL(string: "microblog://post?text=" + encodedString) else {
|
||||
return
|
||||
}
|
||||
|
||||
let _ = try? NSWorkspace.shared.open(url, options: [], configuration: [:])
|
||||
}
|
||||
|
||||
@objc func appDidBecomeActive(_ note: Notification) {
|
||||
|
|
Loading…
Reference in New Issue