Cocoa: I want to know the location of the status bar icon on the screen

I want to know the location of the status bar icon on the screen

-(void)awakeFromNib{
    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
    NSBundle *bundle = [NSBundle mainBundle];
    statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"r" ofType:@"png"]];
    statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"rh" ofType:@"png"]]; 
    [statusItem setImage:statusImage];
    [statusItem setAlternateImage:statusHighlightImage];
    [statusItem setTitle:@"APP"];
    [statusItem setToolTip:@"You do not need this..."];
    [statusItem setHighlightMode:YES];

    NSRect rect = [[[statusItem view] window ] frame];

    NSLog(@"%f",rect.origin.y);

}

To do this and failed

NSRect rect = [[[statusItem view] window ] frame];

NSLog(@"%f",rect.origin.y);
+3
source share
1 answer

You have not set a custom view for your status element, so the call viewwill return zero.

I'm going to suggest that the reason you want to find out the location is when you click on the status item.

If you were to perform an action for your status element, it might look like this:

- (IBAction)someAction:(id)sender
{
    NSWindow *window = [[[NSApplication sharedApplication] currentEvent] window];
    NSRect rect = [window frame];

    NSLog(@"%f",rect.origin.y);
}

and then you set the action of the status element as follows:

[statusItem setAction:@selector(someAction:)];

and then whenever you click on your status, you will see something like this in your journal:

2012-04-17 20:40:24.344 test[337:403] 1578.000000

, , (, Matt Gemmell MAAttachedWindow) .

+4

All Articles