I am trying to create a panel that lets the user choose a path to save a file. When the user selects a directory from the panel that shows relative path (i.e. /folder) the URLs
property is contains /folder. When the user selects a directory that shows the full path, URLs
property of panel
contains the full path (i.e. /User/name/folder). How do I ensure the URLs
property will definitely contain the full paths even though the user's panel shows a relative path?
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:NO]; // yes if more than one dir is allowed
NSInteger clicked = [panel runModal];
NSArray<NSURL *> *URLs;
if (clicked == NSFileHandlingPanelOKButton) {
URLs = [panel URLs];
}
else{
URLs = [NSArray arrayWithObject:[NSURL URLWithString:[NSString stringWithFormat:@"file://%s/", getenv("HOME")]]];
}
for (NSURL *url in URLs) { // When user clicks cancel, [panel URLs] is empty
NSString *selectedDirectoryPath = [url.absoluteString substringFromIndex:6];
// NSString *selectedDirectoryPath = [url path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *sourceFilePath = [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), _fileName];
NSString *destFilePath = [NSString stringWithFormat:@"%@%@", selectedDirectoryPath, _fileName];
}
I thought I could use the path instance property on url, but the array is filled once the user clicks OK to a file path with NSFileHandlingPanelOKButton.
Edit: I found a response that suggests to use beginSheetModalForWindow
in NSOpenPanel URL to string , but how do you use this function?