开发者

Show NSOpenPanel in a NSStatusItem app

开发者 https://www.devze.com 2023-01-29 06:58 出处:网络
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 wou开发者_JS百科ld one do this when the app does not utilize an

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 wou开发者_JS百科ld one do this when the app does not utilize an NSWindow?

Thanks.


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


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.


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);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消