NSOpenPanel not closing on el capitan

I am trying to use NSOpenPanel to retrieve URL of the location to save a file , but the dialog never closes. This was working just fine on OSX 10.10 but not on 10.11.

Are there any changes that caused this or is it something that i am doing wrong? Here is the piece of code:-



string

FileSystemUtil::OpenFolderImpl(const std::string& initDir)

{

NSOpenPanel* openDlg = [NSOpenPanel openPanel];


[openDlg setFloatingPanel: YES];

[openDlg setCanChooseFiles:NO];

[openDlg setCanChooseDirectories:YES];

[openDlg setResolvesAliases:NO];

[openDlg setAllowsMultipleSelection:NO];


if (!initDir.empty()) {

NSString *path = [NSString stringWithCString:initDir.c_str()

encoding:[NSString defaultCStringEncoding]];


NSURL *urlPath = [NSURL fileURLWithPath:path];

[openDlg setDirectoryURL:urlPath];

}


if ( [openDlg runModal] == NSOKButton ) {

NSURL *url = [openDlg URL];

NSString *filename = [url path];

return string([filename UTF8String]);

}

return "";

}


PS- the function returns the correct URL, but the dialog window still remains open.

I'm having similar problems using XCode 7.3, OS X 10.11 and Swift. I've created an app where the user clicks a button and selects a file through NSOpenPanel. The code uses the URL selected to get the file's data and name, then processes the data and saves the result somewhere else. With some files, the processing can take several seconds. So far, once the file is selected, the space where the Open File window had been remains blank, covering the app view and everything else, and stays there until the operation is complete. Also, the app's outlets are frozen until the operation finishes. It appears NSOpenPanel isn't handing window control back to the app and the system.


The code goes like this:

@IBAction func processFile(sender: AnyObject) {
     var chosenURL: NSURL?
     let openPanel = NSOpenPanel()
     openPanel.title = "Choose a file"
     openPanel.canChooseDirectories = false
     openPanel.allowsMultipleSelection = false
      
     if openPanel.runModal() == NSFileHandlingPanelOKButton {
          chosenURL = openPanel.URL
     }
     let dataBytes = NSData(contentsOfURL: chosenURL!)
     let fileName = chosenURL!.lastPathCompnent!
     // Remaining code processes dataBytes and fileName


So how can I make NSOpenPanel shut off before the rest of the function continues? Deepanshu G, may we both find answers to our problems.

I found a solution to my Swift problem. Basically, most of the process after 'let dataBytes =…' was made a background process, with UI updates pushed to the main queue:


@IBAction func processFile(sender: AnyObject) {
     var chosenURL: NSURL?
     let openPanel = NSOpenPanel()
     openPanel.title = "Choose a file"
     openPanel.canChooseDirectories = false
     openPanel.allowsMultipleSelection = false
      
     if openPanel.runModal() == NSFileHandlingPanelOKButton {
          chosenURL = openPanel.URL
     }
     // Main process sent to background
     dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) { [unowned self] in
          let dataBytes = NSData(contentsOfURL: chosenURL!)
          let fileName = chosenURL!.lastPathCompnent!
               // UI updates pushed to the main queue
               dispatch_async(dispatch_get_main_queue()) { [unowned self] } in
               self.messageLabel.stringValue = "Processing"
               }
     // Complete remaining code
     } // close background process


I'm not sure how you could adapt this to Objective-C, but maybe it'll give you some clues.

NSOpenPanel not closing on el capitan
 
 
Q