Open multiple text files with different extensions.

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 )
                }
            }
        }
    }

Replies

You are correct: in a sandboxed app, using the open panel gives you access to the item the user chose, and nothing else. A file with the same base name but a different extension is a different item.


There's no really good way to solve this problem in the current form. If you or your app is the source of these multiple files, it might be easier to arrange to create them as a package rather than as individual files. (A package is an item that looks like a single file to the user, but looks like a directory to your app. The component files are inside the directory.)


If that's not possible, one alternative might be to use the open panel to ask the user for the folder containing the files of interest. You will then have permission to access anything inside that folder. This would require, however, that your app provide its own UI for choosing groups of files (by base name). Depending on what's going on, this might be preferable in some ways. For example, if the only files that can be chosen are those in recognizable groups (by extension), then your UI could filter out ineligible files, which might be easier for the user.


The fallback approach, which is certainly not very appealing, is to use the open panel once for each file, regardless of extension.