2018-01-09 06:53:49 +01:00
|
|
|
//
|
|
|
|
// SendToMicroBlogCommand.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/8/18.
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-01-09 07:10:56 +01:00
|
|
|
import Cocoa
|
2018-01-10 07:04:45 +01:00
|
|
|
import Data
|
2018-01-14 19:56:49 +01:00
|
|
|
import RSCore
|
2018-01-09 06:53:49 +01:00
|
|
|
|
|
|
|
// Not undoable.
|
|
|
|
|
2018-01-09 07:10:56 +01:00
|
|
|
final class SendToMicroBlogCommand: SendToCommand {
|
|
|
|
|
2018-01-12 07:18:46 +01:00
|
|
|
let title = NSLocalizedString("Send to Micro.blog", comment: "Send to command")
|
2018-01-09 07:10:56 +01:00
|
|
|
|
2018-01-12 07:18:46 +01:00
|
|
|
var image: NSImage? {
|
|
|
|
return microBlogApp.icon
|
2018-01-09 07:10:56 +01:00
|
|
|
}
|
2018-01-09 06:53:49 +01:00
|
|
|
|
2018-01-14 19:56:49 +01:00
|
|
|
private let microBlogApp = UserApp(bundleID: "blog.micro.mac")
|
2018-01-12 07:18:46 +01:00
|
|
|
|
2018-01-10 07:04:45 +01:00
|
|
|
func canSendObject(_ object: Any?, selectedText: String?) -> Bool {
|
2018-01-09 06:53:49 +01:00
|
|
|
|
2018-01-14 19:56:49 +01:00
|
|
|
microBlogApp.updateStatus()
|
2018-01-12 07:18:46 +01:00
|
|
|
guard microBlogApp.existsOnDisk, let article = (object as? ArticlePasteboardWriter)?.article, let _ = article.preferredLink else {
|
2018-01-10 07:04:45 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-09 06:53:49 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 07:04:45 +01:00
|
|
|
func sendObject(_ object: Any?, selectedText: String?) {
|
|
|
|
|
|
|
|
guard canSendObject(object, selectedText: selectedText) else {
|
|
|
|
return
|
|
|
|
}
|
2018-01-12 07:18:46 +01:00
|
|
|
guard let article = (object as? ArticlePasteboardWriter)?.article else {
|
|
|
|
return
|
|
|
|
}
|
2018-01-14 19:56:49 +01:00
|
|
|
guard microBlogApp.launchIfNeeded(), microBlogApp.bringToFront() else {
|
2018-01-10 07:04:45 +01:00
|
|
|
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 + ")"
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 23:00:06 +01:00
|
|
|
else if let link = article.preferredLink {
|
|
|
|
s = link
|
|
|
|
}
|
2018-01-10 07:04:45 +01:00
|
|
|
|
2018-01-14 18:36:24 +01:00
|
|
|
let urlQueryDictionary = ["text": s]
|
|
|
|
guard let urlQueryString = urlQueryDictionary.urlQueryString() else {
|
2018-01-10 07:04:45 +01:00
|
|
|
return
|
|
|
|
}
|
2018-01-14 18:36:24 +01:00
|
|
|
guard let url = URL(string: "microblog://post?" + urlQueryString) else {
|
2018-01-10 07:04:45 +01:00
|
|
|
return
|
|
|
|
}
|
2018-01-09 06:53:49 +01:00
|
|
|
|
2018-01-10 07:04:45 +01:00
|
|
|
let _ = try? NSWorkspace.shared.open(url, options: [], configuration: [:])
|
2018-01-09 06:53:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 07:10:56 +01:00
|
|
|
|
|
|
|
|