Question

My app puts an NSStatusItem in the OS X menubar. At some point, I want to remove the menubar icon from the system NSStatusBar. (I want to still retain the NSStatusItem at this point, and send messages to it... just not have it shown.)

I'm using this method to remove the statusItem from the statusBar:

[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];

I want to, at some later point, check if the statusItem is currently shown in the statusBar. I would prefer not to keep track of this via a boolean or etc.

I thought this check would work:

if ([[NSStatusBar systemStatusBar] isEqualTo:[statusItem statusBar]])
{
    NSLog(@"statusItem's bar == system bar, before");
}

NSLog(@"removing from systemStatusBar");
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];

if ([[NSStatusBar systemStatusBar] isEqualTo:[statusItem statusBar]])
{
    NSLog(@"statusItem's bar == system bar, after removal");
}

This outputs:

statusItem's bar == system bar, before removal

removing from systemStatusBar

statusItem's bar == system bar, after removal

So there is no apparent change in the statusItem' statusBar.

The NSStatusBar class reference does not appear to contain any applicable methods.

Is there any way to check if a certain NSStatusItem is in the main system bar?

Was it helpful?

Solution

I have found the private property _statusItems.
This is a little category I wrote, I'm not sure if it works, but you can try it out.


Status Bar Category

@implementation NSStatusBar (statusItemCheck)
- (NSArray *)items {
    return [self valueForKey:@"_statusItems"];
}
- (BOOL)statusItemIsShown:(NSStatusItem *)statusItem {
    if ([self items]) {
        NSInteger index = [[self items] indexOfObject:statusItem];
        if (index != -1) return YES;
    }

    return NO;
}
@end

Edit

You should consider adding a BOOL flag, rather than accessing private methods.
My category is only an example, if you want to upload your app to the MAS, you generally shouldn't use private methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top