Xcode is adding un-wanted menu items to the Edit menu in a Mac Application. There is an automatically generated separator bar and an AutoFill menu item with two sub-menu items. Looking at the source code I see that two other items were suppressed in the initialize method of the AppController like this:
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
//Get rid of Special characters menu item in edit menu:
[defaultValues setObject: [NSNumber numberWithBool: YES] forKey: @"NSDisabledCharacterPaletteMenuItem"];
//Get rid of Start Dictation... menu item in edit menu:
[defaultValues setObject: [NSNumber numberWithBool: YES] forKey: @"NSDisabledDictationMenuItem"];
[[NSUserDefaults standardUserDefaults] registerDefaults: defaultValues];
I guessed that there would also be a key like this: @"NSDisabledAutoFillMenuItem" but adding this to the code does nothing so apparently not.
I searched programming forums and posters say that you can fix this by changing the name of the Edit menu to something like (space)Edit but this doesn't work.
I searcher the Developer documentation and the Archives extensively for info about these keys but of course found nothing. Control-clicking on these pops up a menu of ways to search for these but none of them find anything. Control, option and command clicking on these keys results in ”?”.
I can probably get a pointer to the menu and use NSMenu’s methods to delete these but something like the above would be better. Is there any documentation about this anywhere?
I solved the problem like this:
In my AppController’s init method I added an observer to receive an
NSApplicationWillFinishLaunchingNotification
to remove the extra menu items. This won’t work. Xcode adds the extra items after this. You have to add a selector that will be notified by the
NSApplicationDidFinishLaunchingNotification.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver: self selector: @selector(handleAppLaunched:) name: @"NSApplicationDidFinishLaunchingNotification" object: nil];
The method to remove the extra menus looks like this:
- (void) handleAppLaunched: (NSNotification *) note
{
NSMenu* edit = [[[[NSApplication sharedApplication] mainMenu] itemWithTitle: @"Edit"] submenu];
while( [[[edit itemAtIndex: [edit numberOfItems] - 1] title] compare: @"Paste" ] != 0) //-1 for zero indexing
[edit removeItemAtIndex: [edit numberOfItems] -1]; //decrements the number of Items
}
The undocumented API for removing these with the user defaults is a cleaner way to do this.
It’s pretty ridiculous that there are un-documented APIs and that there is no straight forward way to do this, like a build setting or object method, since this is something you would want to do for a lot of applications.