4

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

1 Answers1

8

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).

omz
  • 53,243
  • 5
  • 129
  • 141
  • Hello, I need to get apps in the status bar. Icons such as dropbox, battery status, etc.... I want to list them in a listbox. I changed your code to list all windows but dropbox and other apps don't appear in the result – Jonathan Chacón Feb 04 '12 at 09:00
  • @JonathanChacón, did you find the solution? – jimwan Nov 16 '17 at 02:37