I accidentally removed "edit" menu in the NSMenu of my OS X app.
Now neither Cmd+X nor Cmd+V works.
I did not find a special 'edit' menu in the interface builder's toolbox.
What's the best way to bring back my beloved edit menu?
Easiest way is to create a new Main Menu template.
Go to File->New->File...
Select OS X -> User Interface -> Main Menu
This will generate a new MainMenu.xib for you, which includes your edit menu.
Just copy paste the edit menu view from the new file into your old MainMenu, and you're good
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.
Long time iOS developer who's dipping his toes in the macOS world. Just trying to better understand how Mac apps work.
For non-storyboard applications based on a XIB, the template creates a default xib and inside is the application's main menu.
However, if I delete that XIB, then create a new one, how do I set up a menu to be the application's main menu? It seems to ignore whatever I create. Plus, when I add one, the graphic looks different than the original; here it's stacked vertically whereas the original had them stacked horizontally.
So, how can you create a second xib (in case you deleted the first one) and replace the main menu in it?
You have to do two steps to change the main menu to a menu in another XIB file:
Change the Main Interface in Deployment Info in the General tab of the Target of your application to the new XIB file.
Open the source code of the new XIB file and search for your new Main Menu. This is a XML tag named menu. Add an attribute named systemMenu with the value main to it. This should look like:
<menu title="Second Menu" systemMenu="main" id="usV-GH-tFG" userLabel="New Main Menu">
Now, your app should use the new Main Menu as System Menu. Some submenus should also have systemMenu attributes with the values apple, services, recentDocuments, font or window. If you have made this change, the display in the Interface Builder also changes.
Building a new menu takes a little effort because you have to create quite a few submenus and entries.
But this way you can easily make mistakes, so you should better create a new Main Menu by copying an existing one.
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 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.