Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tim997

macrumors newbie
Original poster
Jan 13, 2010
3
0
I'm having some problems using NSOpenPanel. I have an open file dialog, which I want to only display/select files of a given type. If I use runModalForTypes everything works just fine. However, this function is deprecated and setAllowedFileTypes & runModal should be used instead. Using the new functions though does not work (all files are displayed rather than only the allowed types) and I can't figure out why. Can anyone please enlighten me?

Here's the code:

Code:
NSOpenPanel* openDlg = [ NSOpenPanel openPanel ];
NSArray* fileTypes = [ NSArray arrayWithObject:@"obj" ];

[ openDlg setAllowedFileTypes:fileTypes ]; // DOES NOT WORK
[ openDlg runModal ];

[ openDlg runModalForTypes:fileTypes ]; // WORKS

I tried also setting setAllowsOtherFileTypes:NO, but this had no effect.

Thanks for any help!
 
Is that the exact code you're using? Are you testing this on 10.6 or a different OS?

I just tested this, and it worked fine (under 10.6.2)
Code:
NSOpenPanel *o = [NSOpenPanel openPanel];
[o setAllowedFileTypes:[NSArray arrayWithObject:@"mp3"]];
[o runModal];
 
Is that the exact code you're using? Are you testing this on 10.6 or a different OS?

I just tested this, and it worked fine (under 10.6.2)
Code:
NSOpenPanel *o = [NSOpenPanel openPanel];
[o setAllowedFileTypes:[NSArray arrayWithObject:@"mp3"]];
[o runModal];

To sanity check, I copied and pasted your code. Same problem. :(

I'm under 10.5.8.
 
Edit: ok, so basically that method doesn't do anything for an NSOpenPanel prior to 10.6.

From http://developer.apple.com/mac/library/releasenotes/Cocoa/AppKit.html
Applications that link on SnowLeopard can now use allowedFileTypes/setAllowedFileTypes:, which was previously not used for the NSOpenPanel. Please refer to the header comments for more information.

NSSavePanel.h on 10.6 says:
NSOpenPanel: On versions less than 10.6, this property is ignored. For applications that link against 10.6 and higher, this property will determine which files should be enabled in the open panel. Using the deprecated methods to show the open panel (the ones that take a "types:" parameter) will overwrite this value, and should not be used. The allowedFileTypes can be changed while the panel is running (ie: from an accessory view). The file type can be a common file extension, or a UTI. This is also known as the "enabled file types". A nil value indicates that all files should be enabled.

So, if you really don't want to use that method, you'll have to do a check at runtime for SL.
 
Edit: ok, so basically that method doesn't do anything for an NSOpenPanel prior to 10.6.

From http://developer.apple.com/mac/library/releasenotes/Cocoa/AppKit.html


NSSavePanel.h on 10.6 says:


So, if you really don't want to use that method, you'll have to do a check at runtime for SL.

Ah, I was just coming back to post an "is it a 10.5 vs 10.6 issue?" query but it seems you've already answered me.

You'll have to forgive me, I'm used to coding under Windows. So if I get past compiling and linking, I just assume the functionality of my code is working & available.

I'm fine with using the older function - i just hate not knowing why something's not working, even when it's a trivial issue like this.

Thanks for the response :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.