Suggested save name of an untitled NSDocument
Asked Answered
M

2

6

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?

Menswear answered 6/3, 2012 at 7:51 Comment(0)
I
4

In OSX 10.8 you can add this method

- (NSString *)defaultDraftName

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nsdocument_Class/Reference/Reference.html

Intrust answered 28/3, 2013 at 14:36 Comment(0)
L
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?

Luik answered 10/3, 2012 at 22:9 Comment(2)
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.