What's special about an ampersand in an NSUndoManager action name?

138 Views Asked by At

I notice that if I say something like

[self.undoManager setActionName: @"X & Y"];

then the resulting menu item looks like "Undo X Y" (with a double space between X and Y). (Tested on Mac OS X 10.7.5 and 10.10.3.) I can make the ampersand show up by doubling it, but I wonder why it happens? Is it documented? Is there some wider set of characters that are specially interpreted in action names or in menu items?

1

There are 1 best solutions below

2
On BEST ANSWER

Back in the NeXT days, menu items could have a "mnemonic", which was a character that appeared underlined in the menu. If the user typed that character while the menu had focus, that item would be selected.

In code, the mnemonic was indicated in a menu title string by prefixing the character with an ampersand. There are still deprecated methods on NSMenuItem to set and get the mnemonic.

OS X doesn't support the feature. It doesn't show the character underlined nor does it let you select the menu item by typing it. So, those methods just strip the ampersand from the title.

Apparently, NSUndoManager must still be using one of those mnemonic-based APIs to set the menu item title. That's presumably for backward compatibility. Some apps may be using ampersands in undo action names with the intent that they be converted to mnemonics. If NSUndoManager stopped stripping them (by switching to more common, non-mnemonic methods of NSMenuItem), then those apps might start showing ampersands in weird places.