3
votes

I am trying to get the on screen location of an NSStatusItem so that I can perform a click on that area via code like below. I am doing this so that my users can press a hotkey to see the menu.

event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, newLocation, kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);

Does anyone know of a way to get the location?, I have been trying ideas and searching for days and have found several ideas but none of them seem to work in leopard/snow leopard

The NSStatusItem is using an NSMenu not a custom view.

2
There's nothing public for that, and it probably shouldn't be done in the first place.Azeem.Butt

2 Answers

3
votes

Don't do this by simulating a mouse event, that is completely the wrong way to go.

You can use the -popUpStatusItemMenu: method of NSStatusItem to show the menu. You can call this method from a hot key by using a CGEventTap to capture global events that your NSStatusItem can respond to.

On 10.6 you can use the +addGlobalMonitorForEventsMatchingMask:handler: method of NSEvent to capture global events instead of using CGEventTap directly.

0
votes

Thinking out loud you could create your own custom view for the NSStatusItem. Then for example from the AppController:

- (void)awakeFromNib {    
float width = 30.0;
float height = [[NSStatusBar systemStatusBar] thickness];
NSRect viewFrame = NSMakeRect(0, 0, width, height);
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
[statusItem setView:[[[MyCustomView alloc] initWithFrame:viewFrame controller:self] autorelease]];

And to simulate the highlight you'd send a message to your custom view. Then when drawing MyCustomView:

// In MyCustomView.m
- (void)drawRect:(NSRect)rect {
if (clicked) {
    [[NSColor selectedMenuItemColor] set];
    NSRectFill(rect);
}

As well as using -popUpStatusItemMenu:.