Question

How do I get the file URL from the default Save As dialog sheet? I am using a document-based application with the Snow Leopard 10.6 SDK in Xcode.

I have scoured the internet and the Apple Documentation for two days and have only found answers that use unique instances of the Save As dialog sheet; everyone seems to want to reinvent the wheel by making their own File -> Save functionality, but that's not what I'm looking to do -- I've already done that!

For example, I do not want to use this approach, I do not want to REPLACE the default -(void)saveDocumentAs:sender within NSDocument with something like:

- (IBAction)saveFileAs:(id)sender
{
    NSSavePanel *spanel = [NSSavePanel savePanel];
    [spanel setCanCreateDirectories:YES];
    [spanel setCanSelectHiddenExtension:YES];
    [spanel setAllowedFileTypes:[[self currentDocument] writableTypesForSaveOperation:NSSaveAsOperation]];
    [spanel setTreatsFilePackagesAsDirectories:YES];
    [spanel beginSheetModalForWindow: [[[[self currentDocument] windowControllers] objectAtIndex:0] window]  completionHandler:^(NSInteger result)
     {
         if (result == NSFileHandlingPanelOKButton)
         {
             NSString *type = [[self currentDocument] fileTypeFromLastRunSavePanel];
             NSLog(@"%@", type);
             NSURL *saveURL = [spanel URL];
             NSLog(@"%@", saveURL);
             [[self currentDocument] dataOfType:type error:nil];

         }
     }];
}

Rather, all I want is the file URL that the user chose for their file from the default NSSavePanel sheet. Because, as you can see from this test, fileTypeFromLastRunSavePanel does not work within the block, so it's either get the file type with the default save panel and not the file URL, or it's get the file URL from a custom save panel and not the file type... at least not with fileTypeFromLastRunSavePanel.

  1. User opens a file
  2. User modifies the file
  3. User does "Save As..."
  4. User types in a new name for the file and presses "Save"
  5. I need the new name for the file. How do I get it without making my own instance of savePanel?

EDIT: I tried [self fileURL] within the NSDocument subclass's

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError

method, but it returns null unless the document has already been saved to disk.

EDIT 2: Just to really specifically clarify what I am asking here, How do I get the URL the user chose from the default NSSavePanel savePanel, not my own instance of NSSavePanel's savePanel? Can I do this without making a subclass of NSSavePanel and overriding its methods? I thought it would make sense that you would be given some sort of URL reference to the file the user selected in the default savePanel without having to add that functionality to your own NSSavePanel instance.

Was it helpful?

Solution

NSSavePanel *panel = [NSSavePanel savePanel];

[panel setMessage:@"Please select a path to create a new database."]; // Message inside modal window
[panel setAllowedFileTypes:[[NSArray alloc] initWithObjects:@"sqlite3", @"sqlite", @"db", nil]];
[panel setAllowsOtherFileTypes:YES];
[panel setExtensionHidden:NO];
[panel setCanCreateDirectories:YES];
[panel setTitle:@"Create a database"]; // Window title
[panel setNameFieldStringValue:@"Untitled.sqlite3"];

[panel beginWithCompletionHandler:^(NSInteger result){
    if (result == NSFileHandlingPanelOKButton) {
        path = [[panel URL] path];
        url = [panel URL];
    }
}];

OTHER TIPS

There is no such thing as a "default" NSSavePanel. You need to create your own instance.

It's important to note that -[NSSavePanel savePanel] is not a singleton like many similar Cocoa API's, it returns a unique instance. You must store a reference to the save panel instance in a variable and access it while configuring the save panel properties. You also access the URL property of the same save panel instance when the save button is clicked.

If you look at the NSSavePanel reference documentation, it clearly shows the URL method, which it states:

Returns the absolute pathname of the file currently shown in the panel as a URL.

This was the first result which came up when I entered "NSSavePanel" into a search engine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top