Photovore
Photovore

Reputation: 155

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

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!

Upvotes: 2

Views: 1089

Answers (1)

Michael Robinson
Michael Robinson

Reputation: 29508

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];

Upvotes: 1

Related Questions