How to get location of NSStatusItem on the screen?

How to get the on-screen location of an NSStatusItem

  • I have a question about the NSStatusItem for cocoa in mac osx. If you look at the mac app called snippets (see the movie at http://snippetsapp.com/). you will see that once you clicked your statusbar icon that a perfectly aligned view / panel or maybe even windows appears just below the icon. My question is ... How to calculate the position to where to place your NSWindow just like this app does? I have tried the following: Subclass NSMenu Set the view popery for the first item of the menu (Worked but enough) Using addSubview instead of icon to NSStatusItem this worked but could not get higher then 20px

  • Answer:

    Give the NSStatusItem a view, then get the frame of that view's window. This technically counts as UndocumentedGoodness, so don't be surprised if it breaks someday (e.g., if they start keeping the window offscreen instead). I don't know what you mean by “could not get heigher then 20px”.

Johnny Mast at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

To do this without the hassle of a custom view, I tried the following (that works). In the method that is set as the action for the status item i.e. the method that is called when the user clicks the status item, the frame of the status item can be retrieved by: [[[NSApp currentEvent] window] frame] Works a treat for me

Steg

It seems that this app uses http://mattgemmell.com/files/source/nsstatusitemtest.zip. There's an sample application with the same layout & position.

Nando Vieira

Is there any way of doing this without replacing the view? A custom view breaks some behaviour, it seems, even if I try to imitade an ordinary menu (like the menu not disappearing when clicking another status item. I tried to create a custom view and swap it, get the frame, and swap back, but that doesn't seem to work: -(NSRect)getFrame; { NSView * swap = [[statusItem view] retain]; // setView calls release on the current view? [statusItem setView:customView]; NSRect frame = [customView getFrame]; // works perfectly [statusItem setView:swap]; // nothing happens :( return frame; }

oskob

From the Apple docs: "Setting a custom view overrides all the other appearance and behavior settings defined by NSStatusItem. The custom view is responsible for drawing itself and providing its own behaviors, such as processing mouse clicks and sending action messages."

Tim

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.