I launch the following runSavePanel() from, e.g.,
@IBAction func JSONall(_ sender: NSButton) {
if !nsPanelActivatedThisSessionForSandbox {
nsPanelActivatedThisSessionForSandbox = true
runSavePanel() }
...
}
where var nsPanelActivatedThisSessionForSandbox = false is declared as a global in the AppDelegate.swift file and is intended to prevent repeated user prompting as the program creates many files but needs sandbox acknowledgment at least once.
The problem behavior is that upon clicking the OK button, the app merrily creates the files in the chosen directory but the modal window stays open until the app is done creating files (around ten minutes). Here is my runSavePanel() and its (empty) delegate:
func runSavePanel() {
let defaults = UserDefaults.standard
let savePanel = NSSavePanel()
savePanel.canCreateDirectories = true
savePanel.canSelectHiddenExtension = false
savePanel.showsHiddenFiles = false
// savePanel.allowedFileTypes = ["JSON", "csv"] // deprecated
savePanel.directoryURL = defaults.url(forKey: "fileDirectoryPref")
let delegate = OpenSavePanelDelegate() // cannot consolidate with next line
savePanel.delegate = delegate
let result = savePanel.runModal()
if result == NSApplication.ModalResponse.OK {
if savePanel.url != nil {
do {
try savePanel.delegate!.panel!(savePanel, validate: savePanel.url!)
defaults.set(savePanel.url, forKey: "fileDirectoryPref")
// defaults.set(savePanel.url, forKey: "NSNavLastRootDirectory") // does not write
} catch {
print("\(result) ViewController Structures.swift:739 runSavePanel()")
}
}
}
}
class OpenSavePanelDelegate: NSObject, NSOpenSavePanelDelegate {
func panel(_ sender: Any, validate url: URL) throws {
/* In Swift, this method returns Void and is marked with the throws keyword to indicate that it throws an error in cases of failure.
You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.
*/
// throw CustomError.InvalidSelection
}
}
// enum CustomError: LocalizedError {
// case InvalidSelection
//
// var errorDescription: String? {
// get {
// return "\(CustomError.InvalidSelection)"
// }
// }
// }
Idk how to stop the double spacing when pasting my code. Sorry.
The delegate is a requirement but I don't know how to configure it. The app doesn't generate errors... or at least it allows the desired output.
Is the delegate supposed to terminate the modal window somehow?