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

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.

Related

How to enable auto generation of Window MenuItems (e.g. Tile Window to Left/Right of Screen and Open file with checkmark) in macOS menubar

I am currently creating a macOS menubar for an app without using any interface builder (no XIB/NIB files), just pure code. However I was expecting some items to be auto-generated during the start-up of the app. Items like "Start Dictation", "Emoji & Symbols" under Edit menu were existing as well as the "Enter Full Screen" menu item under the View Menu. But when it comes to Window Menu nothing was automatically generated, only the menu items I've set in the code. Do I have to enable some flags or options when instantiating a Window NSMenu so it automatically generates those items? I am new to macOS development so I feel like I am kind of lost. Thanks in advance.
The Window and Help menus are a little special in that they have their own NSApplication properties, so you will need to set them to your menus so that the system will know what they are.
For example, if you just create a window menu and add it to the main, all you will get are the items that you have provided. If you also set it as the application’s windowsMenu, in addition you will get all the stuff for moving, tab support, etc.
Setting NSApp’s helpMenu is similar, where a Spotlight menu item is added to the menu.

Is there anyway to hide menubar items in MacOS?

I know I can "auto hide menu bar" in system preferences, however, what I like to do is hide items like this repository.
https://github.com/dwarvesf/hidden
This repository can hide items on right, but I wonder if I can hide left items (which are application menus).
Any idea is appreciated.
As for application menus on the left, those can't be hidden, I'm pretty sure.
Applications often have the option to enable or disable their menu bar helper app in the main preferences. If that doesn't help, e.g. if it's a full-fledged menu bar app, not just a helper, then to my knowledge the only solutions are Hidden Bar, which you mentioned, and Bartender.
I'm using the latter, and it does a very fine job. You have four options in Bartender's preferences:
Show (default macOS behavior)
Hide (menulet will be hidden in a special Bartender secondary menu bar, accessible via the triple-bullet icon ··· on the far right)
Always show (menulet will be visible in the main macOS menu bar as well as the secondary Bartender menu bar at the same screen position)
Always hide (menulet will be completely hidden)
Some older menulets seem to switch their position occasionally, e.g. after wake-from-sleep, and Bartender isn't yet able to fix the position of BitBar instances. But for the vast majority of menu bar apps and helpers it will work just fine.

macOS main menu sub items don't show

I'm using storyboard to create a macOS app.
For some reason, when running the app, the sub-menu item doesn't show up at all.
The item is enabled and hooked to the first responder just like the original demo items.
Did you drag one of the special menus by mistake? The Clear Menu item makes me wonder if the menu started life as a Recent Items menu and perhaps the change didn't take. You can tell by opening your storyboard as source code and checking for the systemMenu attribute on your Scale menu:
<menu key="submenu" title="Open Recent" systemMenu="recentDocuments" id="...">

update statusbar menu on click mac os x 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.

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