Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOpenPanel - Set file type?

Tags:

cocoa

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.

like image 206
EpicDewd Avatar asked Nov 27 '10 01:11

EpicDewd


People also ask

How to get the open panel object in nsopenpanel?

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.

What is the allowedfiletypes property in nsopenpanel?

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”]).

What is the difference between nsopenpanel and nssavepanel?

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.

What are open and save panels in Linux?

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.


2 Answers

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

}
like image 131
Peter Lapisu Avatar answered Oct 01 '22 21:10

Peter Lapisu


How about [openDlg setAllowedFileTypes:fileTypes];?

like image 39
Cesar A. Rivas Avatar answered Oct 01 '22 22:10

Cesar A. Rivas