I'm trying to configure my NSSavePanel instance to have the default 'where' location be set to the user's Desktop, as opposed to the Documents folder, which is what it is currently. I tried to modify my code based on this accepted SO answer. However, the default 'where' location is still the Documents folder. Can someone tell me what I'm doing wrong?
- (void)saveFile:(NSString *)path extension:(NSString *)extension
{
// Build a save dialog
self.savePanel = [NSSavePanel savePanel];
self.savePanel.allowedFileTypes = @[ extension ];
self.savePanel.allowsOtherFileTypes = NO;
// Hide this window
[self.window orderOut:self];
[self.savePanel setDirectoryURL:[NSURL URLWithString:@"/Users/user/desktop"]];
// Run the save dialog
NSInteger result = [self.savePanel runModal];
if (result == NSFileHandlingPanelOKButton) {
// Build the URLs
NSURL *sourceURL = [NSURL fileURLWithPath:path];
NSURL *destinationURL = self.savePanel.URL;
// Delete any existing file
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if ([fileManager fileExistsAtPath:destinationURL.path]) {
[fileManager removeItemAtURL:destinationURL error:&error];
if (error != nil) {
[[NSAlert alertWithError:error] runModal];
}
}
// Bail on error
if (error != nil) {
return;
}
// Copy the file
[[NSFileManager defaultManager] copyItemAtURL:sourceURL toURL:destinationURL error:&error];
if (error != nil) {
[[NSAlert alertWithError:error] runModal];
}
}
// Cleanup
self.savePanel = nil;
}
Instead of:
[self.savePanel setDirectoryURL:[NSURL URLWithString:@"/Users/user/desktop"]];
Try doing:
[self.savePanel setDirectoryURL:[NSURL fileURLWithPath:@"/Users/user/desktop"]];
making certain to replace user
with the correct user name.