2018-01-11 09:51:25 +01:00
|
|
|
//
|
|
|
|
// NSApplication+Scriptability.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Olof Hellman on 1/8/18.
|
|
|
|
// Copyright © 2018 Olof Hellman. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
2018-01-11 09:51:25 +01:00
|
|
|
import Account
|
2018-01-24 09:06:34 +01:00
|
|
|
import Data
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
|
|
extension NSApplication : ScriptingObjectContainer {
|
2018-03-05 03:43:29 +01:00
|
|
|
|
|
|
|
// MARK: --- ScriptingObjectContainer protocol ---
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
var scriptingClassDescription: NSScriptClassDescription {
|
|
|
|
return NSApplication.shared.classDescription as! NSScriptClassDescription
|
|
|
|
}
|
2018-03-05 03:43:29 +01:00
|
|
|
|
|
|
|
func deleteElement(_ element:ScriptingObject) {
|
|
|
|
print ("delete event not handled")
|
|
|
|
}
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
var scriptingKey: String {
|
|
|
|
return "application"
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:11:52 +01:00
|
|
|
@objc(currentArticle)
|
|
|
|
func currentArticle() -> ScriptableArticle? {
|
|
|
|
var scriptableArticle: ScriptableArticle?
|
|
|
|
if let currentArticle = appDelegate.scriptingCurrentArticle {
|
|
|
|
if let feed = currentArticle.feed {
|
|
|
|
let scriptableFeed = ScriptableFeed(feed, container:self)
|
|
|
|
scriptableArticle = ScriptableArticle(currentArticle, container:scriptableFeed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scriptableArticle
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc(selectedArticles)
|
|
|
|
func selectedArticles() -> NSArray {
|
|
|
|
let articles = appDelegate.scriptingSelectedArticles
|
|
|
|
let scriptableArticles:[ScriptableArticle] = articles.compactMap { article in
|
|
|
|
if let feed = article.feed {
|
|
|
|
let scriptableFeed = ScriptableFeed(feed, container:self)
|
|
|
|
return ScriptableArticle(article, container:scriptableFeed)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scriptableArticles as NSArray
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: --- scriptable elements ---
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
@objc(accounts)
|
|
|
|
func accounts() -> NSArray {
|
|
|
|
let accounts = AccountManager.shared.accounts
|
|
|
|
return accounts.map { ScriptableAccount($0) } as NSArray
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:11:52 +01:00
|
|
|
@objc(valueInAccountsWithUniqueID:)
|
|
|
|
func valueInAccounts(withUniqueID id:String) -> ScriptableAccount? {
|
|
|
|
let accounts = AccountManager.shared.accounts
|
|
|
|
guard let account = accounts.first(where:{$0.accountID == id}) else { return nil }
|
|
|
|
return ScriptableAccount(account)
|
|
|
|
}
|
|
|
|
|
2018-01-24 09:06:34 +01:00
|
|
|
/*
|
|
|
|
accessing feeds from the application object skips the 'account' containment hierarchy
|
|
|
|
this allows a script like 'articles of feed "The Shape of Everything"' as a shorthand
|
|
|
|
for 'articles of feed "The Shape of Everything" of account "On My Mac"'
|
2018-02-08 09:11:52 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
func allFeeds() -> [Feed] {
|
2018-01-24 09:06:34 +01:00
|
|
|
let accounts = AccountManager.shared.accounts
|
|
|
|
let emptyFeeds:[Feed] = []
|
2018-02-08 09:11:52 +01:00
|
|
|
return accounts.reduce(emptyFeeds) { (result, nthAccount) -> [Feed] in
|
2018-01-28 03:50:48 +01:00
|
|
|
let accountFeeds = nthAccount.children.compactMap { $0 as? Feed }
|
2018-01-24 09:06:34 +01:00
|
|
|
return result + accountFeeds
|
|
|
|
}
|
2018-02-08 09:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc(feeds)
|
|
|
|
func feeds() -> NSArray {
|
|
|
|
let feeds = self.allFeeds()
|
2018-01-24 09:06:34 +01:00
|
|
|
return feeds.map { ScriptableFeed($0, container:self) } as NSArray
|
|
|
|
}
|
2018-02-08 09:11:52 +01:00
|
|
|
|
|
|
|
@objc(valueInFeedsWithUniqueID:)
|
|
|
|
func valueInFeeds(withUniqueID id:String) -> ScriptableFeed? {
|
|
|
|
let feeds = self.allFeeds()
|
|
|
|
guard let feed = feeds.first(where:{$0.feedID == id}) else { return nil }
|
|
|
|
return ScriptableFeed(feed, container:self)
|
|
|
|
}
|
2018-01-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|