I have an application that requires authentication. Before the user authenticates only a subset of menu items is present in the menu bar. After the user has successfully authenticated I replace the application’s main menu with [NSApp setMainMenu:aMainMenu]; Unfortunately this removes the Spotlight For Help menu item that allows the user to search for menu items and inside the help book. The menu item is present before I replace the main menu.
How can I get the Spotlight For Help menu item back without replacing specific menu items instead of the whole main menu?
When you resetting your main menu, do it without your new help menu built in. Keep that part separate...
... because after resetting your main menu, you will also need to reset your help menu via [NSApp setHelpMenu:]; (and I've linked Apple's documentation for you). That is what will reinstall Spotlight for Help.
Related
I am looking at this, but the documentation says that it will create a Customize Toolbar... menu.
What I am looking for is a way to have NSToolbar respond to the right click and display context menu where one of the item is "Customize...".
Will this functionality be turned on or I will have to do something extra? If I do, then what?
TIA!
If allowsUserCustomization is YES, the toolbar provides a context menu with a “Customize Toolbar…” item. You don't have to do anything else to make this menu work.
In Xcode's macOS application template, the View menu of the menu bar contains a “Customize Toolbar…” menu item which is enabled or disabled automatically based on the setting of allowsUserCustomization of the front window's toolbar. You don't have to do anything to make this work either.
If your app's main menu doesn't have the “Customize Toolbar…” menu item under any submenu, you can add one yourself by dragging a menu item out of the Objects Library. Connect the menu item's action to First Responder's runToolbarCustomizationPalette:.
In decarbonizing I have now come to creating a menubar programmatically using NSMenu.
Carbon seems to be so nice to add standard items to the application menu: Services, Hide app, Hide Others, Show All, Quit app,
and they are even added using the user’s Preferred Language setting in System Preferences’s Language & Region,
so in German Hide Others is added as Andere ausblenden.
I only had to add the About and Preferences items.
However, it seems that in Cocoa I have to add these standard items myself,
but how can I find out what Hide Others is called in the user’s Preferred Language ?
And what is the Cocoa equivalent of CreateStandardWindowMenu ?
If you have Xcode create a new Cocoa application project, it will make a MainMenu.xib that you can examine. It looks like "Hide Others", "Quit" etc. are just menu items with particular actions assigned, but "Services" seems to have some special magic. Even if you don't want to present a whole menu bar from a nib, you could load it from a nib and then pick out certain menu items and move them to another menu, rename them or whatever. Though I'm not sure I understand what you're trying to accomplish. Even if you don't want to localize, you can use a nib.
To address the last part of your revised question, the NSApplication object has a windowsMenu property, i.e., you could create a menu and say [NSApp setWindowsMenu: myMenu]. But I don't know if that adds items like Bring All to Front or if you must create those yourself.
In storyboard, I see key shortcuts in menu items
But when I run app, this shortcuts remove automatically:
I do not understand why!
You have evidently assigned those shortcuts to another menu item, possibly including a pop-menu in the window. If multiple menus have the same shortcuts assigned, it's arbitrary which will actually have it at run time.
Those menus come from default template.
Your app in background calls validateMenuItem: If the menuItem can't perform action (e.g. copy:) the menuItem remains disabled.
object considered for validation is firstResponder (see below picture). If the firstResponder is nil -> menu item will be disabled. Your app in also checks if the object (firstResponder) responds to menu item selectors.
I have an app which is run from the menu bar only (LSUIElement is set in the info.plist) which means there is no main menu except the menu I attached to the status item. The problem is I would like to enable some edit commands when using an NSTextView (like command-c to copy) but all command keys seem to be disabled since the app is technically not active (I get beeps when trying any key combinations).
Is there anyway to add a standard edit menu somewhere and enable command keys that would redirect to it? I'm thinking there could be a hack to make the app temporarily active or something but I can't figure anything out.
Thanks.
They're not disabled. They're gone, because you deleted them.
You need to put back your Main Menu.
If you use version control, you may be able to resurrect it from the past using that. If not, you'll have to create a new project, copy anything custom from your Main Menu nib (if you even still have one) into that one, and move that nib into your actual project to be your new Main Menu nib.
The Edit menu commands, window-related commands (e.g., Close), and numerous other commands all live in the Main Menu. If you delete the Main Menu, you don't have those features anymore.
Your Main Menu isn't visible in a UI element app, but that's not a reason to delete it—it's a reason to keep it, even though your app won't have the menu bar, because not being visible means that it won't peek out from behind the curtain but will remain there keeping the magic of your keyboard shortcuts working.
I have a non-document Cocoa application with a menubar icon and status menu. I've added an "Open Recent" menu to the status menu in Interface Builder. Populating the menu works just fine:
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:
[NSURL fileURLWithPath:filename]]
Now I would also like to add a second "Open Recent" menu to a context popup menu. How would I create the menu programmatically so that it gets populated with entries automatically as it does for the version in the status menu?
I tried creating a copy of the one in the status menu, but it does not get populated. I assume that NSDocumentController is not aware of the menu (frankly, I don't know how it knows about the one in the status menu).
For reference, the best documentation on the inner workings of the Open Recent menu that I found is this:
http://lapcatsoftware.com/blog/2007/07/10/working-without-a-nib-part-5-open-recent-menu/
Unfortunately, it doesn’t help much with this, because even if you create the menu like this, it will be ignored by NSDocumentController. The menu must exist in the main menu before applicationDidFinishLaunching: call, otherwise it won’t be picked up — and consequently, duplicate ones are ignored too.
What I ended up doing, and what seems to work so far, is this:
The first idea was to pick the corresponding NSMenu from the main menu and attach it into other menus as well, hoping that reference counting will make this work. No such luck, setSubmenu throws if the submenu is already in another NSMenuItem.
So I “reparent” the submenu instead — when I need to show it in another menu, I remove it from the main menu’s Open Recent item and set it as submenu in the new menu. Later, I move it back. It’s an ugly hack, of course, but it gets the job done.