« Happy New Year!Export Youtube Videos to the iPod »

Displaying a File Open Dialog in Cocoa with Objective C

12/29/06

  09:27:23 pm, by Rick   , 135 words  
Categories: Cocoa

Displaying a File Open Dialog in Cocoa with Objective C

Ok, so I've been learning Mac programming using Objective-C. I'm still a horrible rank amateur at this stuff, but here's one thing I've learned. Here is an example of how to display a modal File Open dialog box...


int i; // Loop counter.

// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];

// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];

// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:YES];

// Display the dialog.  If the OK button was pressed,
// process the files.
if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
    // Get an array containing the full filenames of all
    // files and directories selected.
    NSArray* files = [openDlg filenames];

    // Loop through all the files and process them.
    for( i = 0; i < [files count]; i++ )
    {
        NSString* fileName = [files objectAtIndex:i];

        // Do something with the filename.
    }
}

8 comments

Comment from: James
JamesWell I like your code. :D
06/01/08 @ 16:27
Shepard EmersonI like your code too :P
08/16/08 @ 09:08
Comment from: bbr
bbrVery useful piece of code, was looking just for that but kept running into the carbon navdialog junk. This works great!
09/24/08 @ 11:40
Comment from: Phoenixxx
PhoenixxxLiked the code.. Very helpful !!
06/30/09 @ 12:40
Comment from: Axel
AxelNice Dialog thx a lot.
08/26/09 @ 12:13
Comment from: Jm
JmYou legend, ive been looking through apple simple browser and complex browser examples, I knew there had to be an easier way
02/24/10 @ 18:03
Comment from: Aneeque
AneequeHi, can you please let me know how can I display the choose application dialog. I have an application in which I am displaying the right click context menu and I have an option in that menu on click of which choose application dialog gets open, my problem is that I don't know how to open what are the carbon/cocoa API's through which this dialog gets displayed.
03/16/10 @ 03:10
Tristan@Aneeque: Set the Default Directory of the FIle DIalog to "/Applications" and then change the only allowed file types to ".app"
04/08/10 @ 22:40
December 2014
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Copyright © 2005 - 2014, Rick Ekle

Comments? Contact me at rick@ekle.us or visit me on Twitter at @rekle

Search

  XML Feeds

powered by free blog software