I need to open multiple result data files from a simulation. They have the same base name but different extensions. I would like to select one model file and then just change the file extension to open the remainder of the files. All of the files are in the same directory. All the files are simple ascii text files.
I'm using the following code as a simple example. When I run this in OSX 10.13.2 (Beta) it opens the file selected in the NSOpenDialog() call but not the other one. With the second file I get the following error:
The file “FLO1.DVA” couldn’t be opened because you don’t have permission to view it.
It's as if the NSOpenPanel() is providing read permissions to the file selected and then when I change the extension and try and open a different file in the same directory read permissions have not been set. Is this what is happening? How do I open the other files with other extensions? I really don't want to have to select each of the data files in an NSOpenPanel() call!
@IBAction func OpenDVAFile(_ sender: Any) {
let dialog = NSOpenPanel();
dialog.title = "Choose a .dva or .tsn file";
dialog.showsResizeIndicator = true;
dialog.showsHiddenFiles = false;
dialog.canChooseDirectories = true;
dialog.canCreateDirectories = true;
dialog.allowsMultipleSelection = false;
dialog.allowedFileTypes = ["dva", "tsn"];
if (dialog.runModal() == NSApplication.ModalResponse.OK) {
let result = dialog.url
if (result != nil) {
do {
let baseFile = result?.deletingPathExtension()
let fileManager = FileManager.default
if let tsnFileURL = baseFile?.appendingPathExtension("TSN") {
if fileManager.fileExists(atPath: tsnFileURL.path) {
print( tsnFileURL, "Exists!")
let tsnFileString = try String( contentsOf: tsnFileURL)
print( "Open TSN Success!")
}
}
if let dvaFileURL = baseFile?.appendingPathExtension("DVA") {
if fileManager.fileExists(atPath: dvaFileURL.path) {
print( dvaFileURL, "Exists!")
let dvaFileString = try String( contentsOf: dvaFileURL)
print( "Open DVA Success!")
}
}
}
catch {
print( error.localizedDescription )
}
}
}
}