mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
basic readonly scripting support for account, feed, folder
This commit is contained in:
95
Evergreen/Scriptability/Account+Scriptability.swift
Normal file
95
Evergreen/Scriptability/Account+Scriptability.swift
Normal file
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// Account+Scriptability.swift
|
||||
// Evergreen
|
||||
//
|
||||
// Created by Olof Hellman on 1/9/18.
|
||||
// Copyright © 2018 Olof Hellman. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import Account
|
||||
import Data
|
||||
|
||||
@objc(ScriptableAccount)
|
||||
class ScriptableAccount: NSObject, UniqueIdScriptingObject, ScriptingObjectContainer {
|
||||
|
||||
let account:Account
|
||||
init (_ account:Account) {
|
||||
self.account = account
|
||||
}
|
||||
|
||||
@objc(objectSpecifier)
|
||||
override var objectSpecifier: NSScriptObjectSpecifier? {
|
||||
let myContainer = NSApplication.shared
|
||||
let scriptObjectSpecifier = myContainer.makeFormUniqueIDScriptObjectSpecifier(forObject:self)
|
||||
return (scriptObjectSpecifier)
|
||||
}
|
||||
|
||||
// MARK: --- ScriptingObject protocol ---
|
||||
|
||||
var scriptingKey: String {
|
||||
return "accounts"
|
||||
}
|
||||
|
||||
// MARK: --- UniqueIdScriptingObject protocol ---
|
||||
|
||||
// I am not sure if account should prefer to be specified by name or by ID
|
||||
// but in either case it seems like the accountID would be used as the keydata, so I chose ID
|
||||
|
||||
var scriptingUniqueId:Any {
|
||||
return account.accountID
|
||||
}
|
||||
|
||||
// MARK: --- ScriptingObjectContainer protocol ---
|
||||
|
||||
var scriptingClassDescription: NSScriptClassDescription {
|
||||
return self.classDescription as! NSScriptClassDescription
|
||||
}
|
||||
|
||||
// MARK: --- Scriptable elements ---
|
||||
|
||||
@objc(feeds)
|
||||
var feeds:NSArray {
|
||||
let feeds = account.children.flatMap { $0 as? Feed }
|
||||
return feeds.map { ScriptableFeed($0, container:self) } as NSArray
|
||||
}
|
||||
|
||||
@objc(folders)
|
||||
var folders:NSArray {
|
||||
let folders = account.children.flatMap { $0 as? Folder }
|
||||
return folders.map { ScriptableFolder($0, container:self) } as NSArray
|
||||
}
|
||||
|
||||
// MARK: --- Scriptable properties ---
|
||||
|
||||
@objc(contents)
|
||||
var contents:NSArray {
|
||||
var contentsArray:[AnyObject] = []
|
||||
for child in account.children {
|
||||
if let aFeed = child as? Feed {
|
||||
contentsArray.append(ScriptableFeed(aFeed, container:self))
|
||||
} else if let aFolder = child as? Folder {
|
||||
contentsArray.append(ScriptableFolder(aFolder, container:self))
|
||||
}
|
||||
}
|
||||
return contentsArray as NSArray
|
||||
}
|
||||
|
||||
@objc(accountType)
|
||||
var accountType:OSType {
|
||||
var osType:String = ""
|
||||
switch self.account.type {
|
||||
case .onMyMac:
|
||||
osType = "Locl"
|
||||
case .feedly:
|
||||
osType = "Fdly"
|
||||
case .feedbin:
|
||||
osType = "Fdbn"
|
||||
case .feedWrangler:
|
||||
osType = "FWrg"
|
||||
case .newsBlur:
|
||||
osType = "NBlr"
|
||||
}
|
||||
return osType.FourCharCode()
|
||||
}
|
||||
}
|
||||
62
Evergreen/Scriptability/Feed+Scriptability.swift
Normal file
62
Evergreen/Scriptability/Feed+Scriptability.swift
Normal file
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// Feed+Scriptability.swift
|
||||
// Evergreen
|
||||
//
|
||||
// Created by Olof Hellman on 1/10/18.
|
||||
// Copyright © 2018 Olof Hellman. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Account
|
||||
import Data
|
||||
|
||||
@objc(ScriptableFeed)
|
||||
class ScriptableFeed: NSObject, UniqueIdScriptingObject {
|
||||
|
||||
let feed:Feed
|
||||
let container:ScriptingObjectContainer
|
||||
|
||||
init (_ feed:Feed, container:ScriptingObjectContainer) {
|
||||
self.feed = feed
|
||||
self.container = container
|
||||
}
|
||||
|
||||
@objc(objectSpecifier)
|
||||
override var objectSpecifier: NSScriptObjectSpecifier? {
|
||||
let scriptObjectSpecifier = self.container.makeFormUniqueIDScriptObjectSpecifier(forObject:self)
|
||||
return (scriptObjectSpecifier)
|
||||
}
|
||||
|
||||
// MARK: --- ScriptingObject protocol ---
|
||||
|
||||
var scriptingKey: String {
|
||||
return "feeds"
|
||||
}
|
||||
|
||||
// MARK: --- UniqueIdScriptingObject protocol ---
|
||||
|
||||
// I am not sure if account should prefer to be specified by name or by ID
|
||||
// but in either case it seems like the accountID would be used as the keydata, so I chose ID
|
||||
|
||||
var scriptingUniqueId:Any {
|
||||
return feed.feedID
|
||||
}
|
||||
|
||||
// MARK: --- Scriptable properties ---
|
||||
|
||||
@objc(url)
|
||||
var url:String {
|
||||
return self.feed.url
|
||||
}
|
||||
|
||||
@objc(uniqueId)
|
||||
var uniqueId:String {
|
||||
return self.feed.feedID
|
||||
}
|
||||
|
||||
@objc(name)
|
||||
var name:String {
|
||||
return self.feed.name ?? ""
|
||||
}
|
||||
|
||||
}
|
||||
56
Evergreen/Scriptability/Folder+Scriptability.swift
Normal file
56
Evergreen/Scriptability/Folder+Scriptability.swift
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Folder+Scriptability.swift
|
||||
// Evergreen
|
||||
//
|
||||
// Created by Olof Hellman on 1/10/18.
|
||||
// Copyright © 2018 Olof Hellman. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Account
|
||||
|
||||
@objc(ScriptableFolder)
|
||||
class ScriptableFolder: NSObject, UniqueIdScriptingObject {
|
||||
|
||||
let folder:Folder
|
||||
let container:ScriptingObjectContainer
|
||||
|
||||
init (_ folder:Folder, container:ScriptingObjectContainer) {
|
||||
self.folder = folder
|
||||
self.container = container
|
||||
}
|
||||
|
||||
@objc(objectSpecifier)
|
||||
override var objectSpecifier: NSScriptObjectSpecifier? {
|
||||
let scriptObjectSpecifier = self.container.makeFormUniqueIDScriptObjectSpecifier(forObject:self)
|
||||
return (scriptObjectSpecifier)
|
||||
}
|
||||
|
||||
// MARK: --- ScriptingObject protocol ---
|
||||
|
||||
var scriptingKey: String {
|
||||
return "folders"
|
||||
}
|
||||
|
||||
// MARK: --- UniqueIdScriptingObject protocol ---
|
||||
|
||||
// I am not sure if account should prefer to be specified by name or by ID
|
||||
// but in either case it seems like the accountID would be used as the keydata, so I chose ID
|
||||
|
||||
var scriptingUniqueId:Any {
|
||||
return folder.folderID
|
||||
}
|
||||
|
||||
// MARK: --- Scriptable properties ---
|
||||
|
||||
@objc(uniqueId)
|
||||
var uniqueId:Int {
|
||||
return self.folder.folderID
|
||||
}
|
||||
|
||||
@objc(name)
|
||||
var name:String {
|
||||
return self.folder.name ?? ""
|
||||
}
|
||||
|
||||
}
|
||||
30
Evergreen/Scriptability/NSApplication+Scriptability.swift
Normal file
30
Evergreen/Scriptability/NSApplication+Scriptability.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// NSApplication+Scriptability.swift
|
||||
// Evergreen
|
||||
//
|
||||
// Created by Olof Hellman on 1/8/18.
|
||||
// Copyright © 2018 Olof Hellman. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import Account
|
||||
|
||||
extension NSApplication : ScriptingObjectContainer {
|
||||
|
||||
var scriptingClassDescription: NSScriptClassDescription {
|
||||
return NSApplication.shared.classDescription as! NSScriptClassDescription
|
||||
}
|
||||
|
||||
var scriptingKey: String {
|
||||
return "application"
|
||||
}
|
||||
|
||||
@objc(accounts)
|
||||
func accounts() -> NSArray {
|
||||
let accounts = AccountManager.shared.accounts
|
||||
return accounts.map { ScriptableAccount($0) } as NSArray
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
22
Evergreen/Scriptability/ScriptingObject.swift
Normal file
22
Evergreen/Scriptability/ScriptingObject.swift
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// ScriptingObject.swift
|
||||
// Evergreen
|
||||
//
|
||||
// Created by Olof Hellman on 1/10/18.
|
||||
// Copyright © 2018 Olof Hellman. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol ScriptingObject {
|
||||
var objectSpecifier: NSScriptObjectSpecifier? { get }
|
||||
var scriptingKey: String { get }
|
||||
}
|
||||
|
||||
protocol NamedScriptingObject: ScriptingObject {
|
||||
var name:String { get }
|
||||
}
|
||||
|
||||
protocol UniqueIdScriptingObject: ScriptingObject {
|
||||
var scriptingUniqueId:Any { get }
|
||||
}
|
||||
38
Evergreen/Scriptability/ScriptingObjectContainer.swift
Normal file
38
Evergreen/Scriptability/ScriptingObjectContainer.swift
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// ScriptingObjectContainer.swift
|
||||
// Account
|
||||
//
|
||||
// Created by Olof Hellman on 1/9/18.
|
||||
// Copyright © 2018 Olof Hellman. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
protocol ScriptingObjectContainer: ScriptingObject {
|
||||
var scriptingClassDescription:NSScriptClassDescription { get }
|
||||
}
|
||||
|
||||
|
||||
extension ScriptingObjectContainer {
|
||||
|
||||
func makeFormNameScriptObjectSpecifier(forObject object:NamedScriptingObject) -> NSScriptObjectSpecifier? {
|
||||
let containerClassDescription = self.scriptingClassDescription
|
||||
let containerScriptObjectSpecifier = self.objectSpecifier
|
||||
let scriptingKey = object.scriptingKey
|
||||
let name = object.name
|
||||
let specifier = NSNameSpecifier(containerClassDescription:containerClassDescription,
|
||||
containerSpecifier:containerScriptObjectSpecifier, key:scriptingKey, name:name)
|
||||
return specifier
|
||||
}
|
||||
|
||||
func makeFormUniqueIDScriptObjectSpecifier(forObject object:UniqueIdScriptingObject) -> NSScriptObjectSpecifier? {
|
||||
let containerClassDescription = self.scriptingClassDescription
|
||||
let containerScriptObjectSpecifier = self.objectSpecifier
|
||||
let scriptingKey = object.scriptingKey
|
||||
let uniqueId = object.scriptingUniqueId
|
||||
let specifier = NSUniqueIDSpecifier(containerClassDescription:containerClassDescription,
|
||||
containerSpecifier:containerScriptObjectSpecifier, key:scriptingKey, uniqueID: uniqueId)
|
||||
return specifier
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user