Thanks, I changed to allowFileTypes to only one in the NSSavePanel ; now it works when I write to desktop directory as follows:
let documentsUrl = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask)[0] as URL // .documentDirectory
let fileName = fromNSPanelURL!.lastPathComponent
let fileUrl = documentsUrl.appendingPathComponent(fileName)
do {
try data.write(to: fileUrl, options: [.atomic])
} catch {
Swift.print(error)
}
But now, I need to create a companion file, with a different name (automatically built, not asked to user), in the same directory.
I tried to follow advices you gave me in another thread :
"1. You must use NSSavePanel to request the location from the user. The user can choose any folder (subject to normal access restrictions such as file permissions based on the login user ID).
2. The URL you get back from the NSSavePanel is a special secure URL. You can use that URL (that instance of that URL object) to form a URL for the file you want to write there (and AFAIK you can use any folder nested inside the folder that the user chose).
3. I believe (but I'm not sure) that the system caches this security information, so you should be able to use a path matching the URL, but you should be using URLs exclusively these days, so this should not be an issue."
The point is that I do not want to ask a second time NSSavePanel for the companion.
I tried to do it with NSOpenPanel, asking for directory only, but I do not understand how to connect the 2.
I wrote this, but I miss something to go on : companion file is not written, there is a flaw somewhere in my logic
@IBAction func newDossier(_ sender: NSMenuItem) {
let openPanel = NSOpenPanel()
openPanel.message = "Choose a directory to save"
openPanel.prompt = "Select"
openPanel.canChooseFiles = false
openPanel.canChooseDirectories = true
openPanel.begin() {
(result2) -> Void in
if (result2 == NSFileHandlingPanelOKButton) {
// Now I open the NSSavePanel
let savePanel = NSSavePanel()
savePanel.title = "File to save"
savePanel.prompt = "Save"
savePanel.allowedFileTypes = ["xxxx"]
let fileManager = FileManager.default
savePanel.begin() { (result) -> Void in
if (result == NSFileHandlingPanelOKButton) {
let fileWithExtensionURL = savePanel.url! // with extension xxxx
if fileManager.fileExists(atPath: fileWithExtensionURL.path) {
let fileName = fileWithExtensionURL.deletingPathExtension().lastPathComponent
let folderName = fileWithExtensionURL.deletingLastPathComponent().lastPathComponent
self.errorExistingFile(fileName: fileName, folderName: folderName)
} else {
let companionFileUrl = // Built programmatically
let _ = saveFile() // This works, because it is the selected file in NSSave
let _ = saveCompanion() // How to make it work ? That' where I need to connect with authorization given by NSOpenPanel ?????
// I hideFileExtension
}
} // result == NSModalResponseOK
} // beginWithCompletionHandler
}
}
}
In addition, to write to a directory different from desktop, I understand I need to pass the url I got from NSOPenPanel ?