How can I tell NSOpenPanel to respect the ⌘A (select all items) key binding?

107 Views Asked by At

MacVim is a macOS application with a file open dialog that calls NSOpenPanel with a simple view to show hidden files (see MMAppController.m's fileOpen method and Miscellaneous.m's showHiddenFilesView method).

When I open files with MacVim, I can use ⌘A to select all files. But I don't see what it's doing that's any different than what I do below, which does not allow me to use ⌘A to select all files.

NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:YES];
if ([panel runModal] == NSModalResponseOK) {
    for ( NSURL* URL in [panel URLs] ) {
        NSLog( @"%@", [URL path] );
    }
} else {
    NSLog( @"ok button not pressed");
}

The docs for NSOpenPanel and NSSavePanel, which NSOpenPanel inherits don't make any mention of key bindings or other booleans that I might set. The only thing that I saw that might be useful is a custom view but MacVim doesn't appear to be doing anything with NSView that relates.

Below is the entire MCV example and how it's compiled. I suspect there's something wrong with the example since I have to click the graphics rectangle first before I can get the open panel to behave. But I don't think that's having an effect on the key bindings.

#import <Cocoa/Cocoa.h>

void openDialog () {
    @try {
        NSOpenPanel *panel = [NSOpenPanel openPanel];
        [panel setCanChooseFiles:YES];
        [panel setCanChooseDirectories:YES];
        [panel setAllowsMultipleSelection:YES];
        if ([panel runModal] == NSModalResponseOK) {
            for ( NSURL* URL in [panel URLs] ) {
                NSLog( @"%@", [URL path] );
            }
        } else {
            NSLog( @"ok button not pressed");
        }
    } @catch (NSException *exception) {
        NSLog(@"%@", [exception callStackSymbols]);
    }
}

void setup () {
    NSWindow *myWindow;
    NSRect    graphicsRect = NSMakeRect(100.0, 350.0, 400.0, 400.0);

    myWindow = [ [NSWindow alloc]
               initWithContentRect: graphicsRect
                         styleMask:NSWindowStyleMaskTitled
                                  |NSWindowStyleMaskClosable
                                  |NSWindowStyleMaskMiniaturizable
                           backing:NSBackingStoreBuffered
                             defer:NO ];

    [myWindow setTitle:@"Open File App"];
    [myWindow makeKeyAndOrderFront: nil];
    openDialog();
}

int main (  ) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSApp = [NSApplication sharedApplication];
    setup();
    [NSApp run];
    [NSApp release];
    [pool release];
    return(EXIT_SUCCESS);
}

gcc -o $@ -framework Cocoa -isysroot $(SDK) -fobjc-exceptions -std=c99 ofdtest.m

EDIT 1

My macOS version is Sonoma 14.2.1. My Xcode version is 15.2 (15C500b). I'm using SDK /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

I'm running the built MCV in iTerm2 but I don't think that matters.

0

There are 0 best solutions below