简体   繁体   中英

How to get all NSStatusItem elements of NSStatusBar in OSX?

I need get all elements in the status bar in OSX.

I tried to get the NSStatusBar id of the System: [NSStatusBar systemStatusBar] but I don't know how can I get all NSStatusItems in it. I found a private method named _items in NSStatusBar but I can't call it:

[[NSStatusBar systemStatusBar] _items];

Xcode tould me that that method doesn't exist.

How can I get all NSStatusItem elements in the NSStatusBar?

Thanks

You cannot get all items as NSStatusItem objects because they don't all belong to your process.

If you're only interested where they are on screen and which apps own them, you can do that with the CGWindow APIs, because technically the status items are (borderless) windows. Here's an example that logs information about all status bar items:

NSArray *windowInfos = (NSArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID); 
for (NSDictionary *windowInfo in windowInfos) {
    if (([[windowInfo objectForKey:(id)kCGWindowLayer] intValue] == 25) 
        && (![[windowInfo objectForKey:(id)kCGWindowOwnerName] isEqual:@"SystemUIServer"])) {
        NSLog(@"Status bar item: %@", windowInfo);
    }
}
[windowInfos release];

Note that the system's items are not included; they are all combined in one window that belongs to "SystemUIServer". Also, this method might not be particularly reliable because the window layer for status bar items might change (it's assumed to be 25 here, but this is not documented anywhere).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM