update statusbar menu on click mac os x cocoa - cocoa

I've been playing around with a statusbar menu app and what I'd like it to do is to update the content of the menu when it is clicked. I've been able to get everything working on the awakefromnib function and it works as expected. Now i just want to update the menu everytime I click on the status bar. This surely can be done, since it's done in the dropbox statusbar menu and many of the native app statusbar menus. I'm not able to associate a action directly with the click of the menu and I don't want to leave a loop running in the background cause I don't really see a need for it. Any ideas? An alternative would be to have an action associated to a menu item which does not cause the menu to get hidden again. Any help would be welcome.

Just set a delegate for your menu, and implement the -(void)menuWillOpen:(NSMenu *)menu method.

Related

How do you make a button automatically press without user interaction in Swift Cocoa

This sounds like a weird question, but I am using Swift 2.0 and Xcode to create a cocoa app, and I have a popover linked to an invisible button. I wan't to activate it so it pops up after the user has been on the page for a little while. I have a timer set, but I can't figure out the code to get the button to actually activate at that time, so that the popover will show. I am using a button so I can have an access spot for the popover to show at.
Right now, if you click the (invisible) button, everything shows perfectly. However, I need the click to activate programmatically. I would really appreciate any help!
Thanks!

Swift - NSMenu closes when clicking outside of menu

so i have a simple NSMenu. It is attached to a Status Item Button.
When i click the Button it pops Open.
Now my Problem is that as soon as i click outside of the NSMenu, it closes. How can i avoid that ?
I did try many things but cant get that to work.
The reason i need that is that when the user clicks something in the NSMenu, another window opens, and while this window is open i also want the NSMenu to stay open. But it just does not work.
I feel like its impossible.
Many Thanks in advance.
I believe you have to create a custom view that pops up but something that could be done natively with just NSMenu.

How can I get to the menu bar in my app in interface builder

XCode works in mysterious ways (at least to me).
I simply want to create a Preference pane in my app. When I run my app, the stock menu bar comes up (Apple, MyApp, File, Edit...) and the "Preferences" menu item is grayed out. It makes sense since I haven't started playing with it.
How on earth do I add/remove/activate/inactivate menu items? I'm not talking about adding anything new, simply using what should be there.
Thanks in advance.
NSMenu has "Auto Enable Items" enabled by default. That means if the menu item does not have it's action message hooked up, it will appear grayed out. So in your case, you would simply set the Preferences menu item's "Sent Action" to whatever action shows your preferences window. This can be hooked to some sort of showPreferencesWindow: method of your AppDelegate, or directly to the showWindow: method of a window controller.
To dynamically enable/disable menu items the best way is to implement the NSUserInterfaceItemValidation protocol which is excellently documented here
Edit: Your app's menu bar items live in the MainMenu.xib file. The menu bar appears as a "Main Menu" object on the left hand side (if you're using Xcode 4) Simply click on the items to modify them, and you can Ctrl+drag connections to and from them like any UI object.

Programmatically controlling a Cocoa Button on OSX (making it invisible and pressed)

I am trying to interface with a cocoa popup menu from an OpenGL button. The actual button needs to be in OpenGL and I cannot stick an actual Cocoa button in its place but when this button is pressed I would like for a Cocoa menu to popup just like the one that comes up when you press the nspopupbutton. It seems that there is no way to get this popup window to come up by itself so I wanted to just insert an invisible button and have it be pressed automatically when the popup menu method is called. Is there a way to programmatically set a button to pressed (this will bring up the menu from the popupbutton) and make the button itself invisible without making the resulting menu invisible?
It seems that there is no way to get this popup window to come up by itself
Sure there is. See + popUpContextMenu:withEvent:forView:
Have you thought about just using a regular NSMenu, rather than trying to finagle the menu from an NSPopupButton?

how do you display an NSMenu programmatically?

I wrote a little app that lives in the NSStatusBar. I want to have a global shortcut that when hit, the menu bar's content is displayed, exactly like the behavior of spotlight.
I have added a global key shortcut to my application, but I am unable to get the meun bar to display. How can I do this? I tried with "popUpContextMenu" , but that method displays the menu in the bottom left hand corner, I want the menu to open up right under the NSStatusBar menu icon.
You can do it, apple provides a method for it popUpStatusItemMenu:
I don't think there is a supported way to do this without perhaps using the Accessibility framework to simulate a click on your status item. Regardless, it's probably not a good idea to abuse a menu in this way.
The Spotlight menu bar item does not use an NSMenu, it uses a custom window/view. You might consider going this route if you have some sort of custom view to display.

Resources