Files
NetNewsWire/Mac/Scripting/NSApplication+Scriptability.swift
T

144 lines
4.6 KiB
Swift
Raw Normal View History

//
// NSApplication+Scriptability.swift
2018-08-28 22:18:24 -07:00
// NetNewsWire
//
// Created by Olof Hellman on 1/8/18.
// Copyright © 2018 Olof Hellman. All rights reserved.
//
import AppKit
import Account
import Articles
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 {
return self.classDescription as! NSScriptClassDescription
}
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")
}
nonisolated var scriptingKey: String {
return "application"
}
2025-11-05 11:22:10 -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) {
scriptableArticle = ScriptableArticle(currentArticle, container: scriptableFeed)
}
}
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)
} else {
return nil
}
}
return scriptableArticles as NSArray
}
2025-11-05 11:22:10 -08:00
@objc(countOfSelectedArticles)
func countOfSelectedArticles() -> Int {
return appDelegate.scriptingSelectedArticles.count
}
2025-11-05 11:22:10 -08: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) {
return ScriptableArticle(article, container: scriptableFeed)
} else {
return nil
}
}
// MARK: --- scriptable elements ---
@objc(accounts)
func accounts() -> NSArray {
2019-08-01 21:52:12 -07:00
let accounts = AccountManager.shared.accounts
return accounts.map { ScriptableAccount($0) } as NSArray
}
2025-11-05 11:22:10 -08:00
@objc(countOfAccounts)
func countOfAccounts() -> Int {
return AccountManager.shared.accounts.count
}
2025-11-05 11:22:10 -08: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
@objc(valueInAccountsWithUniqueID:)
2026-01-02 10:17:41 -08:00
func valueInAccounts(withUniqueID id: String) -> ScriptableAccount? {
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
}
return ScriptableAccount(account)
}
/*
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"'
*/
2025-11-05 11:22:10 -08:00
2025-11-03 09:42:00 -08:00
func allFeeds() -> [Feed] {
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)
return result + accountFeeds
}
}
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
}
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
}
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)
}
}