I am writing a method that puts up an NSOpenPanel so that the user can select an app of their choice from their hard-drive (or other volumes.)
From the NSOpenPanel docs:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre>& amp;lt;font size=1 face=courier>runModalForDirectory:file:type s:
- (int)runModalForDirectory

NSString *)directory file

NSString *)filename types

NSArray *)fileTypes</font></pre><HR></BLOCKQUOTE>
Displays the receiver and begins a modal event loop that is terminated when the user clicks either OK or Cancel, resulting in the return of NSOKButton or NSCancelButton, respectively. The receiver displays the files in directory (an absolute directory path) that match the types in fileTypes (an NSArray of file extensions). If directory is nil the default directory is the application directory. If all files in a directory should appear in the browser, fileTypes should be nil. You can control whether directories and files appear in the browser with the setCanChooseDirectories: and setCanChooseFiles: methods. The filename argument specifies a particular file in directory that is selected when the Open panel is presented to the user; otherwise, filename should be nil.
there is also a shorter method more appropriate: runModalForTypes: which is the same as above but directory is nil and file is nil(i.e. we start in the Apps directory with no file pre-selected.)
This method works fine (passing filetypes an array with just one entry - ".app")
The exception - If the user selects an alias to a monolithic app (i.e. an app that doesn't use a bundle (e.g. interarchy.))
If this happens the NSOpenPanel thinks that the alias points to a directory (normally fair since an alias to an app usually points to a directory ending in .app) and when the user tries to select the alias you get the following (carbon) error:
FSOpenIterator failed:-1407
which from the docs equates to
errFSNotAFolder -1407 : 'A parameter was expected to identify a folder, but it identified some other kind of object (e.g., a file) instead. This implies that the specified object exists, but is of the wrong type. For example, one of the parameters to FSCreateFileUnicode is an FSRef of the directory where the file will be created; if the FSRef actually refers to a file, this error is returned.'
So is this a bug in my code (below) or is this a bug in NSOpenPanel?
Help! Any Ideas? I have posted this to the cocoa-dev list, but they didn't seem very interested.
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre>& amp;lt;font size=1 face=courier>
<font color = brown> NSArray *fileTypes = [NSArray arrayWithObject:<font color = orange>@"app"</font>]; </font>
<font color = brown>// We Only want to be able to select apps</font>
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[oPanel setAllowsMultipleSelection:NO];
[oPanel setResolvesAliases:NO]; <font color = brown>// This doesn't affect the problem</font>
result = [oPanel runModalForTypes: fileTypes];
<font color = brown>// equivalent to runModalForDirectory:file:types: method, using nil for both the filename and directory arguments.</font>
<font color = brown>// filename is the pre-selected file.</font>
<font color = brown>// directory is the starting directory - nil means the Applications Directory. </font>
if (result == NSOKButton) <font color = brown>// If we didn't press cancel.</font>
{
NSArray *filesToOpen = [oPanel filenames];
NSString *aFile = [filesToOpen objectAtIndex:<font color = blue>0</font>]; <font color = brown>// aFile is the path to the file we want. </font>
doStuffWithPath…
} <font color = brown>// if (result == NSOKButton)</font>
} <font color = brown>// - (IBAction)setProtocolInfo

id)sender</font>
</font></pre><HR></BLOCKQUOTE>
[ 12-09-2001: Message edited by: Diggory Laycock ]
[ 12-12-2001: Message edited by: Diggory Laycock ]
[ 12-12-2001: Message edited by: Diggory Laycock ]