cocoa:I want to know the location of the status bar icon on the screen
cocoa, macos, objective-c, xcode
Solution
You haven't set a custom view for your status item, so calling `view` is going to return nil.
I'm going to guess that the reason you want to know the location is when you click the status item.
If you were to implement an action for your status item, 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'd set your status item's action like this:
[statusItem setAction:@selector(someAction:)];
and then whenever you clicked your status item, you'd see something ilke this in your log:
2012-04-17 20:40:24.344 test[337:403] 1578.000000
You could use that information to, for example, position a window (like Matt Gemmell's MAAttachedWindow) relative to the status item.
Problem
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 so and did not get to ``` NSRect rect = [[[statusItem view] window ] frame]; NSLog(@"%f",rect.origin.y); ```