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
.
- User opens a file
- User modifies the file
- User does "Save As..."
- User types in a new name for the file and presses "Save"
- 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.