How can I have a default file selected for opening in an NSOpenPanel?

1k Views Asked by At

The old way was [ MyPanel runModalForDirectory: thedir file: thefile ], and the panel would open in the specified directory with the specified file already selected.

That's deprecated; we're supposed to use runModal now, but I don't see any way to set the selected file name. I've tried including it in the string for setDirectoryURL, I've tried setRepresentedFilename, I've tried setNameFieldStringValue ... obviously it's possible to do this somehow, but I'm not having any luck with the internet search terms I've chosen, nor with Apple's documentation....

Thanks if you can help!

1

There are 1 best solutions below

5
On

The documentation lists setDirectoryURL: & setNameFieldStringValue: as available in 10.6 and above:

NSString *defaultDirectoryPath, *defaultName;
NSOpenPanel *openPanel;
...
[openPanel setNameFieldStringValue:defaultName];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:defaultDirectoryPath]];
[openPanel runModal];