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?
Suggested save name of an untitled NSDocument
Asked Answered
In OSX 10.8 you can add this method
- (NSString *)defaultDraftName
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?
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. –
Leaky
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. –
Nylon © 2022 - 2024 — McMap. All rights reserved.