Is there a way to suggest a file name for the initial save dialog (of an untitled document) to use for a document in the nsdocument framework?
Asked
Active
Viewed 1,484 times
2 Answers
3
In Mac OS X v10.7 and later:
- (void)setDisplayName:(NSString *)displayNameOrNil
v10.6, override in your NSDocument subclass:
- (BOOL)prepareSavePanel:(NSSavePanel *)savePanel
{
if( [savePanel.nameFieldStringValue isEqualToString:@"Untitled"] )
[savePanel setNameFieldStringValue:@"hello"];
return [super prepareSavePanel:savePanel];
}
In fact the default implementation is empty and returns YES so could just do that.
Not sure about testing for "Untitled" though, won't work if they have already saved as "Untitled" and want to keep that name, and maybe it won't localise, so maybe set a flag in
- (id)initWithType:(NSString *)type error:(NSError **)error
or is there already one?

Steve Rogers
- 1,963
- 2
- 17
- 25
-
Thanks for the suggestion. Going for this implementation on both v10.6 and v10.7. Testing for the fileURL:path to be non-nil to make sure that the file isn't already saved as Untitled.ext. – jkcl Mar 21 '12 at 08:22
-
This is great when you don't immediately know the display name you should use. For example, my app sets the display name of untitled documents to match a title field inside the document; saved documents show the actual file name, of course. `-defaultDraftName` has its uses, but it doesn't have the same flexibility. – Becca Royal-Gordon Apr 14 '13 at 23:58