Search code examples
cocoamacruby

How can I preset the filename in NSSavePanel?


NSSavePanel used to have a runModalForDirectory:file: method which let you preset the directory and filename for a save panel. But that is deprecated in 10.6

When creating an NSSavePanel, how can I preset the filename without using the deprecated method?


Solution

  • Use the setNameFieldStringValue: method, which was added in 10.6, before running the save panel. If you want to set the default directory too, you will need the setDirectoryURL: method, also added in 10.6.

    NSString *defaultDirectoryPath, *defaultName;
    NSSavePanel *savePanel;
    ...
    [savePanel setNameFieldStringValue:defaultName];
    [savePanel setDirectoryURL:[NSURL fileURLWithPath:defaultDirectoryPath]];
    [savePanel runModal];