Just looking on what I would use to only allow specific files to be selected (Images for now)
setFileTypesArray
returns
NSOpenPanel may not respond to -setFileTypesArray:
and then the panel doesn't open up at all. Heres my code:
NSArray * fileTypes = [NSArray arrayWithObjects:@"png",@"tiff",@"baz",nil];
NSLog(@"Button Pressed");
[textField setStringValue:@"Test"];
int i; // Loop counter.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setFileTypesArray:fileTypes];
Thanks.
Use the openPanel class method of NSOpenPanel to retrieve an Open panel object. Present the panel using the beginWithCompletionHandler: method. Use your completion handler to process the results.
NSOpenPanel has an allowedFileTypes property to deal with this problem. The strict type of allowedFileTypes is [String], but with a twist: valid values are a) file extentions ( [“jpg”, “png”]) and b) UTIs ( [“public.jpeg”, “public.png”]).
Whereas NSOpenPanel allows and parses UTIs (“public.image” lets you open all supported image formats, of which there are a lot), NSSavePanel is a little fussier. but you would be quite wrong. As you see when you unhide the the extension.
The standard Open and Save panels provide you with an interface to use whenever you interact with the user’s files. You present the Open panel when you want the user to select one or more existing files or directories.
You may wan to check out
[panel setAllowedFileTypes:[NSImage imageTypes]];
Or implement the delegate NSOpenSavePanelDelegate
and implement
- (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url {
NSString * fileExtension = [url pathExtension];
if (([fileExtension isEqual: @""]) || ([fileExtension isEqual: @"/"]) || (fileExtension == nil)) {
return YES;
}
NSSet * allowed = [NSSet setWithArray:@[@"png", @"tiff", @"jpg", @"gif", @"jpeg"]];
return [allowed containsObject:[fileExtension lowercaseString]];
}
How about [openDlg setAllowedFileTypes:fileTypes];
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With