cocoansstatusitemnsstatusbar

How to get frame for NSStatusItem


Is it possible to get the frame of a NSStatusItem after I've added it to the status bar in Cocoa? When my app is launched, I am adding an item to the system status bar, and would like to know where it was positioned, is possible.


Solution

  • If you have set a custom view on the status item:

    NSRect statusRect = [[statusItem view] frame];
    NSLog(@"%@", [NSString stringWithFormat:@"%.1fx%.1f",statusRect.size.width, statusRect.size.height]);
    

    Otherwise I don't think it's possible using the available and documented APIs.

    Edit: Incorporated comments.