22

In my Application i need to show the select file dialog, I am making use of the NSOpenPanel which allows to select the file, code is as shown below,

- (IBAction)sendFileButtonAction:(id)sender{

    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( int i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:i];
            [self log:fileName];

            // Do something with the filename.
        }
    }

}

everything works perfect, but i am facing only one issue, while opening the file, it shows the Open and Cancel button, Is there any way to rename the open button to “Select” button, or do i need to use some other Cocoa Resource.

Ky -
  • 30,724
  • 51
  • 192
  • 308
Amitg2k12
  • 3,765
  • 10
  • 48
  • 97
  • 2
    By the way, the filenames property is declared deprecated in OS X 10.6 – Max Yankov Jun 09 '12 at 22:50
  • runModalForDirectory:file:types: is deprecated in OS X v10.6. You could use runModal instead. You can set path using setDirectoryURL:, and you can set fileTypes using setAllowedFileTypes:. – Itachi May 28 '13 at 05:35

2 Answers2

13

Add this line:

[openDlg setPrompt:@"Select"];
Anne
  • 26,765
  • 9
  • 65
  • 71
  • Is there a way to have the filename you select diplay onto a label? Or a way to Actually select the file and store it in the application to access it later? Thanks!! –  Jul 30 '11 at 01:11
  • I suppose yes, in the framework you would get callback, delegate for the selected file name and that you could use to display it over the label – Amitg2k12 May 07 '12 at 04:42
  • hey i want to close openPanel on tapping select can you tell me hows it possible @Anne – Gajendra Rawat Jan 07 '14 at 13:08
4

Thanks a lot for the question and answers. I have replaced deprecated methods and it seems to work fine. Sorry not yet sure about editing other people answers (new to contributing here).

 - (IBAction)sendFileButtonAction:(id)sender{

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

    // Change "Open" dialog button to "Select"
    [openDlg setPrompt:@"Select"];

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

        // Loop through all the files and process them.
        for( int i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:i];
            NSLog(@"file: %@", fileName);
            // Do something with the filename.
        }
    }
}
MazarD
  • 2,759
  • 2
  • 22
  • 33
Justas
  • 136
  • 7