fbitterlich fbitterlich - 1 month ago 9
Objective-C Question

How to limit the choice in an NSOpenPanel to a specific filename?

When showing an NSOpenPanel, is there a way to limit the choice of the file to files which have a certain filename? I know you can limit the file types by setting

openPanel.allowedFileTypes
, but for a specific feature the user needs to select a file which is known to have a certain name (
diallist.csv
).

I know I can check after the panel closes, but it seems more user friendly to me to not let the user choose unacceptable files in the first place.

Answer

You can set a delegate for the NSOpenPanel : https://developer.apple.com/reference/appkit/nsopensavepaneldelegate

As delegate you can validate and/or enable certain URLs:

- (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url; or - (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError * _Nullable *)outError;