2018-01-11 00:51:25 -08:00
|
|
|
//
|
|
|
|
|
// NSApplication+Scriptability.swift
|
2018-08-28 22:18:24 -07:00
|
|
|
// NetNewsWire
|
2018-01-11 00:51:25 -08:00
|
|
|
//
|
|
|
|
|
// Created by Olof Hellman on 1/8/18.
|
|
|
|
|
// Copyright © 2018 Olof Hellman. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2018-02-02 22:51:32 -08:00
|
|
|
import AppKit
|
2018-01-11 00:51:25 -08:00
|
|
|
import Account
|
2018-07-23 18:29:08 -07:00
|
|
|
import Articles
|
2018-01-11 00:51:25 -08:00
|
|
|
|
2026-01-02 10:17:41 -08:00
|
|
|
extension NSApplication: @preconcurrency ScriptingObjectContainer {
|
2026-01-01 17:55:12 -08:00
|
|
|
// MARK: - ScriptingObjectContainer protocol
|
2026-01-02 15:05:23 -08:00
|
|
|
|
|
|
|
|
nonisolated var scriptingClassDescription: NSScriptClassDescription {
|
2025-11-18 15:29:29 -08:00
|
|
|
return self.classDescription as! NSScriptClassDescription
|
2018-01-11 00:51:25 -08:00
|
|
|
}
|
2018-03-04 18:43:29 -08:00
|
|
|
|
2026-01-02 10:17:41 -08:00
|
|
|
func deleteElement(_ element: ScriptingObject) {
|
2026-01-02 17:50:35 -08:00
|
|
|
print("delete event not handled")
|
2018-03-04 18:43:29 -08:00
|
|
|
}
|
|
|
|
|
|
2025-11-18 15:29:29 -08:00
|
|
|
nonisolated var scriptingKey: String {
|
2018-01-11 00:51:25 -08:00
|
|
|
return "application"
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2018-02-08 00:11:52 -08:00
|
|
|
@objc(currentArticle)
|
|
|
|
|
func currentArticle() -> ScriptableArticle? {
|
|
|
|
|
var scriptableArticle: ScriptableArticle?
|
|
|
|
|
if let currentArticle = appDelegate.scriptingCurrentArticle {
|
2025-11-02 15:12:00 -08:00
|
|
|
if let feed = currentArticle.feed,
|
2025-11-03 09:42:00 -08:00
|
|
|
let scriptableFeed = ScriptableFeed.scriptableFeed(for: feed) {
|
2025-10-04 16:56:59 -07:00
|
|
|
scriptableArticle = ScriptableArticle(currentArticle, container: scriptableFeed)
|
2018-02-08 00:11:52 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return scriptableArticle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc(selectedArticles)
|
|
|
|
|
func selectedArticles() -> NSArray {
|
|
|
|
|
let articles = appDelegate.scriptingSelectedArticles
|
2026-01-02 10:17:41 -08:00
|
|
|
let scriptableArticles: [ScriptableArticle] = articles.compactMap { article in
|
2025-11-02 15:12:00 -08:00
|
|
|
if let feed = article.feed,
|
2025-11-03 09:42:00 -08:00
|
|
|
let scriptableFeed = ScriptableFeed.scriptableFeed(for: feed) {
|
2026-01-02 10:17:41 -08:00
|
|
|
return ScriptableArticle(article, container: scriptableFeed)
|
2018-02-08 00:11:52 -08:00
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return scriptableArticles as NSArray
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2025-10-05 11:20:42 -07:00
|
|
|
@objc(countOfSelectedArticles)
|
|
|
|
|
func countOfSelectedArticles() -> Int {
|
|
|
|
|
return appDelegate.scriptingSelectedArticles.count
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2025-10-05 11:20:42 -07:00
|
|
|
@objc(objectInSelectedArticlesAtIndex:)
|
|
|
|
|
func objectInSelectedArticlesAtIndex(_ index: Int) -> ScriptableArticle? {
|
|
|
|
|
let articles = appDelegate.scriptingSelectedArticles
|
|
|
|
|
guard index >= 0 && index < articles.count else { return nil }
|
|
|
|
|
let article = articles[index]
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2025-11-02 15:12:00 -08:00
|
|
|
if let feed = article.feed,
|
2025-11-03 09:42:00 -08:00
|
|
|
let scriptableFeed = ScriptableFeed.scriptableFeed(for: feed) {
|
2025-10-05 11:20:42 -07:00
|
|
|
return ScriptableArticle(article, container: scriptableFeed)
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-08 00:11:52 -08:00
|
|
|
|
|
|
|
|
// MARK: --- scriptable elements ---
|
|
|
|
|
|
2018-01-11 00:51:25 -08:00
|
|
|
@objc(accounts)
|
|
|
|
|
func accounts() -> NSArray {
|
2019-08-01 21:52:12 -07:00
|
|
|
let accounts = AccountManager.shared.accounts
|
2018-01-11 00:51:25 -08:00
|
|
|
return accounts.map { ScriptableAccount($0) } as NSArray
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2025-10-05 11:13:59 -07:00
|
|
|
@objc(countOfAccounts)
|
|
|
|
|
func countOfAccounts() -> Int {
|
|
|
|
|
return AccountManager.shared.accounts.count
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2025-10-05 11:13:59 -07:00
|
|
|
@objc(objectInAccountsAtIndex:)
|
|
|
|
|
func objectInAccountsAtIndex(_ index: Int) -> ScriptableAccount? {
|
|
|
|
|
let accounts = Array(AccountManager.shared.accounts)
|
|
|
|
|
guard index >= 0 && index < accounts.count else { return nil }
|
|
|
|
|
return ScriptableAccount(accounts[index])
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2018-02-08 00:11:52 -08:00
|
|
|
@objc(valueInAccountsWithUniqueID:)
|
2026-01-02 10:17:41 -08:00
|
|
|
func valueInAccounts(withUniqueID id: String) -> ScriptableAccount? {
|
2024-11-23 11:15:20 -08:00
|
|
|
let accounts = AccountManager.shared.accounts
|
2026-01-02 10:17:41 -08:00
|
|
|
guard let account = accounts.first(where: { $0.accountID == id }) else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-02-08 00:11:52 -08:00
|
|
|
return ScriptableAccount(account)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 00:06:34 -08: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 00:11:52 -08:00
|
|
|
*/
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2025-11-03 09:42:00 -08:00
|
|
|
func allFeeds() -> [Feed] {
|
2019-05-02 06:01:30 -05:00
|
|
|
let accounts = AccountManager.shared.activeAccounts
|
2026-01-02 10:17:41 -08:00
|
|
|
let emptyFeeds: [Feed] = []
|
2025-11-02 14:51:46 -08:00
|
|
|
return accounts.reduce(emptyFeeds) { (result, nthAccount) -> [Feed] in
|
2025-11-02 15:31:54 -08:00
|
|
|
let accountFeeds = Array(nthAccount.topLevelFeeds)
|
2018-01-24 00:06:34 -08:00
|
|
|
return result + accountFeeds
|
|
|
|
|
}
|
2018-02-08 00:11:52 -08:00
|
|
|
}
|
|
|
|
|
|
2025-11-03 09:42:00 -08:00
|
|
|
@objc(feeds)
|
|
|
|
|
func feeds() -> NSArray {
|
2026-01-02 10:17:41 -08:00
|
|
|
allFeeds().map { ScriptableFeed($0, container: self) } as NSArray
|
2018-01-24 00:06:34 -08:00
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2025-11-03 09:42:00 -08:00
|
|
|
@objc(countOfFeeds)
|
|
|
|
|
func countOfFeeds() -> Int {
|
|
|
|
|
allFeeds().count
|
2025-10-05 11:29:37 -07:00
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2025-11-03 09:42:00 -08:00
|
|
|
@objc(objectInFeedsAtIndex:)
|
|
|
|
|
func objectInFeedsAtIndex(_ index: Int) -> ScriptableFeed? {
|
|
|
|
|
let feeds = allFeeds()
|
2026-01-02 10:17:41 -08:00
|
|
|
guard index >= 0 && index < feeds.count else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-11-03 09:42:00 -08:00
|
|
|
return ScriptableFeed(feeds[index], container: self)
|
2025-10-05 11:29:37 -07:00
|
|
|
}
|
2018-02-08 00:11:52 -08:00
|
|
|
|
2025-11-03 09:42:00 -08:00
|
|
|
@objc(valueInFeedsWithUniqueID:)
|
2026-01-02 10:17:41 -08:00
|
|
|
func valueInFeeds(withUniqueID id: String) -> ScriptableFeed? {
|
2025-11-03 09:42:00 -08:00
|
|
|
let feeds = allFeeds()
|
2026-01-02 17:50:35 -08:00
|
|
|
guard let feed = feeds.first(where: { $0.feedID == id}) else {
|
2026-01-02 10:17:41 -08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return ScriptableFeed(feed, container: self)
|
2018-02-08 00:11:52 -08:00
|
|
|
}
|
2018-01-11 00:51:25 -08:00
|
|
|
}
|