In our Cocoa NSDocument-based application we have a Save (saveDocument:) button on a context menu, but we want the text of the menu-item to change depending on the circumstances of the NSDocument, in the same way that the main menu Save (saveDocument:) menu-item changes. Does anyone know a clean solution to update this menu-item text, ideally calling on the same Cocoa functionality that the main menu uses?
Details:
The main menu in a Cocoa NSDocument-based application usually has a Save menu item that fires First Responder - saveDocument:. The title of this menu item changes depending on the circumstances of the document, for example:
In Lion (OS X 10.7) the menu-item text starts out as 'Save', but becomes 'Save a Version' (eg after the document has been saved).
In Mountain Lion (OS X 10.8), the menu item text starts out as 'Save...', but because 'Save' (eg after the document has been saved).
In our app, we display a context menu with a Save menu-item, and so we want the text to change in the same way as in the main menu (listed above). We don't want to have to write code that contains complicated logic (check the NSDocument... check the OSX version and show a string accordingly) and we are really looking to be able to call the same code that the Cocoa main menu uses to change the menu-item title.
Related
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.
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="...">
I have a OS X application. The app works fine, and the first menu item is the app's title, "Elision". However, under the "Elision" and "Help" menus, the title displayed is "Test". How do I change this?
Open the xib or storyboard file containing the menu, and select the menu item(s) you wish to change. You may set the menu item titles as desired via the Inspector:
How can I display an NSPopover above my app's dock icon?
There are two ways to customise the dock menu, based on if your application is running or not. See the Apple documentation for customising it when its open here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/docktasks_cocoa/docktasks_cocoa.html#//apple_ref/doc/uid/TP30000986-CH3-SW1
To summarise, you basically provide a menu in the main application's XIB or storyboard which contains the extra menu options that you want to add to the dock menu.
To customise your Dock icon's menu when the application is not running, see the guide here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/CreatingaDockTilePlug-in/CreatingaDockTilePlug-in.html
Basically, you need to create a plugin which you drop into your application's resource folder. This works very like the original method, but executes inside Finder's dock process.
I have customized the contextual menu for my Cocoa application such that only certain items are visible. In spite of my customization, I found that the system adds a menu item -- "Add to iTunes as a spoken track".
I tried removing this item from the menu but somehow, I am unable to get the control. Is there a way this item can be removed, or do I have to write an AppleScript to disable the iTunes option under Keyboard Shortcuts? I hate doing that since I will then have to restore it for the user.
You can control the contents of these menus (system-wide) by using Preferences.
See System Preferences > Keyboard > Keyboard Shortcuts > Services > Text
In my experience, Apple's default items trigger off of the exact title of the menu. For example, I have a toy app with an NSMenu that I am creating entirely in code (no nib). I find that the View menu gets an extra item (for full screen) if I initialize it as follows:
NSMenu* viewMenu = [[NSMenu alloc]initWithTitle:#"View"];
However, if I put an extra space into the name, then Apple doesn't mess with it:
NSMenu* viewMenu = [[NSMenu alloc]initWithTitle:#"View "];
Is there a way this item can be removed, or do I have to write an AppleScript to disable the iTunes option under Keyboard Shortcuts?
Don't fight with The System; those Service items are supposed to appear on every context menu, as specified in the System Preferences. It even appears on the context menu of Safari which I'm using to view this post right now. Yes I agree that having "add to iTunes" enabled by default is a poor choice on the part of Apple, but that's life.
Maybe the user has his/her own service item s/he installed say as an Automator action. In that case the user probably doesn't want to have it removed.