I’m having an issue with opening directories that have spaces in them. My code looks like this:
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseDirectories:YES];
if ( [openDlg runModal] == NSOKButton )
{
NSArray* files = [openDlg URLs];
NSString* directoryName = [[files objectAtIndex:0] absoluteString];
directoryURL = [files objectAtIndex:0];
NSLog(@"Directory Name: %@", directoryName);
NSArray *directoryArray = [directoryName componentsSeparatedByString:@"/"];
NSString* currentDirectory = [directoryArray objectAtIndex:(directoryArray.count- 2)];
[directoryBox setTitle:currentDirectory];
}
When I select a directory name with spaces the files are not displayed in a table and the output in the NSLog looks like this:
Directory Name:
file://localhost/Users/Rich/Software%20Bisque/
Any ideas?
The
-URLsmethod of ofNSOpenPanelreturns instances ofNSURL, not file system paths. WhileNSURLs have become the preferred way to refer to files, you can easily change to a file system path by usingNSURL‘s-pathmethod.Note that there are many methods specific to working with file system paths that are added to
NSStringinNSPathUtilities.h. You could probably rewrite your code to incorporate those (double-check that I’ve got your targeted directory okay):