2

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;
}
Community
  • 1
  • 1
narner
  • 2,908
  • 3
  • 26
  • 63

1 Answers1

3

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.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • That makes sense! One question---what about making sure this will work for any user? – narner May 22 '15 at 18:27
  • Something like: `[NSURL fileURLWithPath:[NSString stringWithFormat: @"%@/Desktop", NSHomeDirectory()]]`. I can't remember if [`NSHomeDirectory()`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/#//apple_ref/c/func/NSHomeDirectory) appends a trailing slash or not, but you will find out pretty quickly. – Michael Dautermann May 22 '15 at 18:29
  • Brilliant, thanks so much! Marking as correct. Now all I need to do is make sure that the panel will respect a users last choice. Thanks again Michael! – narner May 22 '15 at 18:33
  • 1
    Arguably a more correct way to get the URL would be `[[NSFileManager defaultManager] URLForDirectory: NSDesktopDirectory inDomain: NSUserDomainMask appropriateForURL: nil create: YES error: nil]`. – JWWalker May 22 '15 at 19:56
  • Yeah, I could have provided that as a solution also @JWWalker ... I was just trying to keep the code relatively close to what the OP had to begin with. – Michael Dautermann May 22 '15 at 19:58