2018-01-09 06:53:49 +01:00
//
// S e n d T o M a r s E d i t C o m m a n d . s w i f t
2018-08-29 07:18:24 +02:00
// N e t N e w s W i r e
2018-01-09 06:53:49 +01:00
//
// C r e a t e d b y B r e n t S i m m o n s o n 1 / 8 / 1 8 .
// C o p y r i g h t © 2 0 1 8 R a n c h e r o S o f t w a r e . A l l r i g h t s r e s e r v e d .
//
2018-02-03 07:51:32 +01:00
import AppKit
2018-01-14 20:00:42 +01:00
import RSCore
2018-07-24 03:29:08 +02:00
import Articles
2018-01-09 06:53:49 +01:00
2020-04-07 22:25:33 +02:00
final class SendToMarsEditCommand : ExtensionPoint , SendToCommand {
2018-01-09 06:53:49 +01:00
2020-04-14 23:47:05 +02:00
static var isSinglton = true
2020-04-22 21:16:50 +02:00
static var isDeveloperBuildRestricted = false
2020-04-14 23:47:05 +02:00
static var title = NSLocalizedString ( " MarsEdit " , comment : " MarsEdit " )
2020-10-30 21:42:45 +01:00
static var image = AppAssets . extensionPointMarsEdit
2020-04-14 23:47:05 +02:00
static var description : NSAttributedString = {
2020-11-02 00:29:13 +01:00
let attrString = SendToMarsEditCommand . makeAttrString ( " This extension enables share menu functionality to send selected article text to MarsEdit. You need the MarsEdit application for this to work. " )
2020-04-14 23:47:05 +02:00
let range = NSRange ( location : 81 , length : 8 )
attrString . beginEditing ( )
attrString . addAttribute ( NSAttributedString . Key . link , value : " https://red-sweater.com/marsedit/ " , range : range )
attrString . addAttribute ( NSAttributedString . Key . foregroundColor , value : NSColor . systemBlue , range : range )
attrString . endEditing ( )
return attrString
} ( )
2020-04-08 17:12:06 +02:00
let extensionPointID = ExtensionPointIdentifer . marsEdit
2020-04-15 05:33:05 +02:00
var title : String {
2020-04-15 06:03:08 +02:00
return extensionPointID . extensionPointType . title
2020-04-15 05:33:05 +02:00
}
2018-01-12 07:18:46 +01:00
var image : NSImage ? {
2018-01-14 20:00:42 +01:00
return appToUse ( ) ? . icon ? ? nil
2018-01-12 07:18:46 +01:00
}
2018-01-14 20:00:42 +01:00
private let marsEditApps = [ UserApp ( bundleID : " com.red-sweater.marsedit4 " ) , UserApp ( bundleID : " com.red-sweater.marsedit " ) ]
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 20:00:42 +01:00
if let _ = appToUse ( ) {
2018-01-12 07:18:46 +01:00
return true
}
2018-01-09 06:53:49 +01:00
return false
}
2018-01-10 07:04:45 +01:00
func sendObject ( _ object : Any ? , selectedText : String ? ) {
2018-01-09 06:53:49 +01:00
2018-01-15 21:22:12 +01:00
guard canSendObject ( object , selectedText : selectedText ) else {
2018-01-12 07:18:46 +01:00
return
}
2018-01-15 21:22:12 +01:00
guard let article = ( object as ? ArticlePasteboardWriter ) ? . article else {
return
}
guard let app = appToUse ( ) , app . launchIfNeeded ( ) , app . bringToFront ( ) else {
return
}
send ( article , to : app )
2018-01-12 07:18:46 +01:00
}
}
private extension SendToMarsEditCommand {
2018-01-15 21:22:12 +01:00
func send ( _ article : Article , to app : UserApp ) {
// A p p h a s a l r e a d y b e e n l a u n c h e d .
guard let targetDescriptor = app . targetDescriptor ( ) else {
return
}
let body = article . contentHTML ? ? article . contentText ? ? article . summary
let authorName = article . authors ? . first ? . name
2020-01-13 01:38:04 +01:00
let sender = SendToBlogEditorApp ( targetDescriptor : targetDescriptor , title : article . title , body : body , summary : article . summary , link : article . externalURL , permalink : article . url , subject : nil , creator : authorName , commentsURL : nil , guid : article . uniqueID , sourceName : article . webFeed ? . nameForDisplay , sourceHomeURL : article . webFeed ? . homePageURL , sourceFeedURL : article . webFeed ? . url )
2018-01-15 21:22:12 +01:00
let _ = sender . send ( )
}
2018-01-14 20:00:42 +01:00
func appToUse ( ) -> UserApp ? {
2018-01-12 07:18:46 +01:00
2018-01-14 20:12:28 +01:00
marsEditApps . forEach { $0 . updateStatus ( ) }
2018-01-14 20:00:42 +01:00
for app in marsEditApps {
if app . isRunning {
return app
}
2018-01-14 20:12:28 +01:00
}
for app in marsEditApps {
2018-01-14 20:00:42 +01:00
if app . existsOnDisk {
return app
2018-01-12 07:18:46 +01:00
}
}
return nil
2018-01-09 06:53:49 +01:00
}
}