David
David

Reputation: 14414

Show NSOpenPanel in a NSStatusItem app

I am writing a status item app that does not have an NSWindow. I to pull up an NSOpenPanel when the user clicks on the status item. How would one do this when the app does not utilize an NSWindow?

Thanks.

Upvotes: 1

Views: 562

Answers (3)

Anni S
Anni S

Reputation: 2026

You can simply call your open panel from NSMenuItem's action as:

NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setAllowsMultipleSelection:YES];
    [panel setCanChooseDirectories:YES];

    NSUInteger result = [panel runModal];
    NSMutableArray *paths = [NSMutableArray array];

    if(result == NSFileHandlingPanelOKButton) {
        for (NSURL *url in [panel URLs]) {
            NSLog(@"%@", url);
        }
    }

Upvotes: 0

Extra Savoir-Faire
Extra Savoir-Faire

Reputation: 6036

In your status item's IBAction method, call this:

window = [[NSApp currentEvent] window];

You can then pass that window to NSOpenPanel's beginSheetModalForWindow:completionHandler: in order to display the open panel as a sheet.

You may find that the status item itself curls up and disappears as the sheet appears, but it reappears when you dismiss the sheet.

Upvotes: 1

Dave DeLong
Dave DeLong

Reputation: 243156

Run it as a modal window instead of as a sheet.

Upvotes: 4

Related Questions