2018-01-11 09:51:25 +01:00
|
|
|
//
|
|
|
|
// Folder+Scriptability.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2018-01-11 09:51:25 +01:00
|
|
|
//
|
|
|
|
// Created by Olof Hellman on 1/10/18.
|
|
|
|
// Copyright © 2018 Olof Hellman. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Account
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2024-03-21 04:49:15 +01:00
|
|
|
import Core
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
|
|
@objc(ScriptableFolder)
|
2024-04-17 04:37:53 +02:00
|
|
|
@MainActor class ScriptableFolder: NSObject, UniqueIDScriptingObject, ScriptingObjectContainer {
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
|
|
let folder:Folder
|
|
|
|
let container:ScriptingObjectContainer
|
|
|
|
|
|
|
|
init (_ folder:Folder, container:ScriptingObjectContainer) {
|
|
|
|
self.folder = folder
|
|
|
|
self.container = container
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc(objectSpecifier)
|
2024-04-17 05:53:33 +02:00
|
|
|
nonisolated override var objectSpecifier: NSScriptObjectSpecifier? {
|
|
|
|
MainActor.assumeIsolated {
|
|
|
|
let scriptObjectSpecifier = self.container.makeFormUniqueIDScriptObjectSpecifier(forObject:self)
|
|
|
|
return (scriptObjectSpecifier)
|
|
|
|
}
|
2018-01-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: --- ScriptingObject protocol ---
|
|
|
|
|
|
|
|
var scriptingKey: String {
|
|
|
|
return "folders"
|
|
|
|
}
|
|
|
|
|
2024-04-17 04:37:53 +02:00
|
|
|
// MARK: --- UniqueIDScriptingObject protocol ---
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2024-04-17 04:37:53 +02:00
|
|
|
@objc(uniqueID)
|
|
|
|
var scriptingUniqueID:Any {
|
2018-01-11 09:51:25 +01:00
|
|
|
return folder.folderID
|
|
|
|
}
|
|
|
|
|
2018-03-05 03:43:29 +01:00
|
|
|
// MARK: --- ScriptingObjectContainer protocol ---
|
|
|
|
|
|
|
|
var scriptingClassDescription: NSScriptClassDescription {
|
|
|
|
return self.classDescription as! NSScriptClassDescription
|
|
|
|
}
|
|
|
|
|
2024-06-08 20:58:30 +02:00
|
|
|
func deleteElement(_ element:ScriptingObject) {
|
2024-03-28 17:28:16 +01:00
|
|
|
// TODO: fix this
|
|
|
|
// if let scriptableFeed = element as? ScriptableFeed {
|
|
|
|
// BatchUpdate.shared.perform {
|
|
|
|
// folder.account?.removeFeed(scriptableFeed.feed, from: folder) { result in }
|
|
|
|
// }
|
|
|
|
// }
|
2018-03-05 03:43:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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"}
|
|
|
|
*/
|
2024-06-08 20:58:30 +02:00
|
|
|
class func handleCreateElement(command:NSCreateCommand) -> Any? {
|
2024-03-27 06:18:48 +01:00
|
|
|
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 {
|
|
|
|
print("support for folders within folders is NYI");
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-27 13:38:43 +02:00
|
|
|
command.suspendExecution()
|
2024-03-27 06:18:48 +01:00
|
|
|
|
2024-06-08 20:58:30 +02:00
|
|
|
Task {
|
2024-03-27 06:18:48 +01:00
|
|
|
do {
|
|
|
|
let folder = try await account.addFolder(name)
|
2019-09-27 13:38:43 +02:00
|
|
|
let scriptableAccount = ScriptableAccount(account)
|
|
|
|
let scriptableFolder = ScriptableFolder(folder, container:scriptableAccount)
|
|
|
|
command.resumeExecution(withResult:scriptableFolder.objectSpecifier)
|
2024-03-27 06:18:48 +01:00
|
|
|
} catch {
|
2019-09-27 13:38:43 +02:00
|
|
|
command.resumeExecution(withResult:nil)
|
|
|
|
}
|
|
|
|
}
|
2024-03-27 06:18:48 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-05 03:43:29 +01:00
|
|
|
// MARK: --- Scriptable elements ---
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
@objc(feeds)
|
|
|
|
var feeds:NSArray {
|
|
|
|
let feeds = Array(folder.topLevelFeeds)
|
|
|
|
return feeds.map { ScriptableFeed($0, container:self) } as NSArray
|
2018-03-05 03:43:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
// MARK: --- Scriptable properties ---
|
|
|
|
|
|
|
|
@objc(name)
|
|
|
|
var name:String {
|
|
|
|
return self.folder.name ?? ""
|
|
|
|
}
|
|
|
|
|
2018-01-20 07:49:01 +01:00
|
|
|
@objc(opmlRepresentation)
|
|
|
|
var opmlRepresentation:String {
|
2020-01-14 05:20:20 +01:00
|
|
|
return self.folder.OPMLString(indentLevel:0)
|
2018-01-20 07:49:01 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
}
|