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;
}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

That makes sense! One question---what about making sure this will work for any user?
Something like: [NSURL fileURLWithPath:[NSString stringWithFormat: @"%@/Desktop", NSHomeDirectory()]]. I can't remember if NSHomeDirectory() appends a trailing slash or not, but you will find out pretty quickly.
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!
Arguably a more correct way to get the URL would be [[NSFileManager defaultManager] URLForDirectory: NSDesktopDirectory inDomain: NSUserDomainMask appropriateForURL: nil create: YES error: nil].
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.