NetNewsWire/Mac/Scriptability/Folder+Scriptability.swift

117 lines
3.3 KiB
Swift
Raw Normal View History

//
// Folder+Scriptability.swift
2018-08-28 22:18:24 -07:00
// NetNewsWire
//
// Created by Olof Hellman on 1/10/18.
// Copyright © 2018 Olof Hellman. All rights reserved.
//
import Foundation
import Account
import Articles
import RSCore
@objc(ScriptableFolder)
class ScriptableFolder: NSObject, UniqueIdScriptingObject, ScriptingObjectContainer {
2025-01-22 22:17:02 -08:00
let folder: Folder
let container: ScriptingObjectContainer
2025-01-22 22:17:02 -08:00
init (_ folder: Folder, container: ScriptingObjectContainer) {
self.folder = folder
self.container = container
}
@objc(objectSpecifier)
override var objectSpecifier: NSScriptObjectSpecifier? {
2025-01-22 22:17:02 -08:00
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
@objc(uniqueId)
2025-01-22 22:17:02 -08:00
var scriptingUniqueId: Any {
return folder.folderID
}
2025-01-22 22:17:02 -08:00
// MARK: --- ScriptingObjectContainer protocol ---
2025-01-22 22:17:02 -08:00
var scriptingClassDescription: NSScriptClassDescription {
return self.classDescription as! NSScriptClassDescription
}
2025-01-22 22:17:02 -08:00
func deleteElement(_ element: ScriptingObject) {
2024-11-02 11:08:58 -07:00
if let scriptableFeed = element as? ScriptableFeed {
BatchUpdate.shared.perform {
2025-01-22 22:17:02 -08:00
folder.account?.removeFeed(scriptableFeed.feed, from: folder) { _ in }
}
}
}
// MARK: --- handle NSCreateCommand ---
/*
handle an AppleScript like
make new folder in account X with properties {name:"new folder name"}
or
tell account X to make new folder at end with properties {name:"new folder name"}
*/
2025-01-22 22:17:02 -08:00
class func handleCreateElement(command: NSCreateCommand) -> Any? {
guard command.isCreateCommand(forClass: "fold") else { return nil }
let name = command.property(forKey: "name") as? String ?? ""
// some combination of the tell target and the location specifier ("in" or "at")
// identifies where the new folder should be created
let (account, folder) = command.accountAndFolderForNewChild()
guard folder == nil else {
2025-01-22 22:17:02 -08:00
print("support for folders within folders is NYI")
return nil
}
2025-01-22 22:17:02 -08:00
command.suspendExecution()
2025-01-22 22:17:02 -08:00
account.addFolder(name) { result in
switch result {
case .success(let folder):
let scriptableAccount = ScriptableAccount(account)
2025-01-22 22:17:02 -08:00
let scriptableFolder = ScriptableFolder(folder, container: scriptableAccount)
command.resumeExecution(withResult: scriptableFolder.objectSpecifier)
case .failure:
2025-01-22 22:17:02 -08:00
command.resumeExecution(withResult: nil)
}
}
2025-01-22 22:17:02 -08:00
return nil
}
2025-01-22 22:17:02 -08:00
// MARK: --- Scriptable elements ---
2025-01-22 22:17:02 -08:00
2024-11-02 11:08:58 -07:00
@objc(feeds)
2025-01-22 22:17:02 -08:00
var feeds: NSArray {
2024-11-01 22:09:22 -07:00
let feeds = Array(folder.topLevelFeeds)
2025-01-22 22:17:02 -08:00
return feeds.map { ScriptableFeed($0, container: self) } as NSArray
}
// MARK: --- Scriptable properties ---
2025-01-22 22:17:02 -08:00
@objc(name)
2025-01-22 22:17:02 -08:00
var name: String {
return self.folder.name ?? ""
}
@objc(opmlRepresentation)
2025-01-22 22:17:02 -08:00
var opmlRepresentation: String {
return self.folder.OPMLString(indentLevel: 0)
}
}