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-09 06:53:49 +01:00
|
|
|
|
|
|
|
// Not undoable.
|
|
|
|
|
2018-01-09 07:10:56 +01:00
|
|
|
final class SendToMicroBlogCommand: SendToCommand {
|
|
|
|
|
|
|
|
private let bundleID = "blog.micro.mac"
|
|
|
|
private var appExists = false
|
|
|
|
|
|
|
|
init() {
|
|
|
|
|
|
|
|
self.appExists = appExistsOnDisk(bundleID)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive(_:)), name: NSApplication.didBecomeActiveNotification, object: nil)
|
|
|
|
}
|
2018-01-09 06:53:49 +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-10 23:00:06 +01:00
|
|
|
guard appExists, let article = object as? 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
|
|
|
|
}
|
|
|
|
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 + ")"
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 23:00:06 +01:00
|
|
|
else if let link = article.preferredLink {
|
|
|
|
s = link
|
|
|
|
}
|
2018-01-10 07:04:45 +01:00
|
|
|
|
|
|
|
guard let encodedString = s.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let url = URL(string: "microblog://post?text=" + encodedString) else {
|
|
|
|
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
|
|
|
@objc func appDidBecomeActive(_ note: Notification) {
|
2018-01-09 06:53:49 +01:00
|
|
|
|
2018-01-09 07:10:56 +01:00
|
|
|
self.appExists = appExistsOnDisk(bundleID)
|
2018-01-09 06:53:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 07:10:56 +01:00
|
|
|
|
|
|
|
|