What are some complete examples of what one would include in a FrontEnd init.m that would make use of FrontEndAddMenuCommands to add commands to the Mathematica system menus (without having to directly edit MenuSetup.tr?

For example, to do the following:

1. Add an item to the Help menu that brings up the DocumentationCenter window for Installed AddOns.
2. Add an item to the Format > Background Color sub-menu that would add a new color?

Yes, I've already consulted MathGroup threads going back to 2009, and earlier, on modifying the startup menu by editing MenuSetup.tr and about evaluating, during a session, an expression to do a FrontEndExecute. But those messages don't seem to give a complete example of what I'm asking here.

-
Is FrontEndAddMenuCommands really a command of Mathematica? Because I wasn't able to find it. The problem is, from what I have gathered, that the only way to add new commands to the menu is by hacking the .tr file. If it was an already established menu command you could have used the tokens to access it. Also, you can always make a palette with whatever functionality you wish ... – Spawn1701D Jan 29 '12 at 2:58
FrontEndAddMenuCommands may be obsolete now but seems to have been an actual, if undocumented, command in earlier versions of Mathematica (perhaps it was really FrontEndExecute.... ?). This appeared in forums.wolfram.com/mathgroup/archive/2009/Apr/msg00341.html and other posts in that same thread. – murray Jan 29 '12 at 4:54

I've never seen that command before but this does work at least in version 7 on Windows:

FrontEndExecute[
"BackgroundDialog", {Delimiter,
Item["L&inen", Background -> RGBColor[0.980, 0.941, 0.902]],
Item["A&liceBlue", Background -> RGBColor[0.941, 0.973, 1.0]],
Item["Min&tCream", Background -> RGBColor[0.961, 1.0, 0.980]],
Item["Lig&htYellow", Background -> RGBColor[1.0, 1.0, 0.878]],
Item["Smok&e", Background -> RGBColor[0.961, 0.961, 0.961]],
Item["Mi&styRose", Background -> RGBColor[1.0, 0.894, 0.882]]}]]


That comes from this MathGroup post. (Now improved; see comments.)

You can completely reset the menus using...

FrontEndExecute[FrontEndResetMenusPacket[{Automatic}]]

-
How does one control where in a particular menu the additions go. I'm surprised, for example, to see that the command shown to add background colors causes those new colors to be listed at the top of the drop-down Background Color menu rather than at the bottom. Is it possible for such a command to add items in the middle of an existing menu -- in the case of the Background Color menu, e.g., to appear between the Black...White group and the Light Blue... group? – murray Jan 29 '12 at 19:21
@murray good question; I'll play with this later today and see what I can determine. – Mr.Wizard Jan 29 '12 at 21:15
Glad to see my vintage 2003 answers are getting reposted here! :) But, if I were to write the answer today, I would not use LinkWrite[$ParentLink, ...]. I would instead use FrontEndExecute[...]. FrontEndExecute uses MathLinkCallFrontEnd (which you could also use) rather than LinkWrite to communicate with the FE. MathLinkCallFrontEnd was added to v6 to correctly deal with the multiple links between FE and kernel. The code here risks failure, for example, if it's called from a Dynamic or Button. – John Fultz Feb 1 '12 at 14:36 @murray, the only way to target AddMenuCommand is to target a menu command which, in MenuSetup.tr, is implemented using a simple FrontEndToken. Which is to say that its structure in MenuSetup.tr looks like MenuItem[_, _String, ___]. It's a silly limitation, but that's the way it is right now. The only menu item in the Background Color menu which fits that description is the "Palette..." item, which is how the aforementioned example works. – John Fultz Feb 1 '12 at 14:45 @John about time an expert showed up; thanks! – Mr.Wizard Feb 1 '12 at 16:53 This can be accomplished using LinkWrite. You can place the following code in init.m. To add a color to the background colors submenu you can use... LinkWrite[$ParentLink,
"BackgroundDialog", {Delimiter,
Item["M&y Color", Background -> RGBColor[0.980, 0.941, 0.902]]}]]


LinkWrite[\$ParentLink,
FrontEndExecute[{FrontEndNotebookOpen[
FrontEndFindFileOnPath["paclet:guide/InstalledAddOns",
"PrivatePathsSystemResources"]]}]]}]]


EDIT: Controlling the placement with this method seems hard since there is no documentation on the topic. It seems that AddMenuCommands takes a front end token as the first input and then attempts to find the first instance in MenuSetup.tr and replace immediately after that. I've spent about all the time I think its worth trying to figure out more and have come to the conclusion that if you want highly customized menus you will probably need to edit MenuSetup.tr directly.

End EDIT:

Now, if you want to actually edit MenuSetup.tr the following will accomplish the same as the above.

You can accomplish part 1 by adding the following code after line 628 of MenuSetup.tr which is located under /WolframResearch/Mathematica/8.0/SystemFiles/FrontEnd/TextResources/Windows in my case.

,
Delimiter,
FrontEndExecute[{FrontEndNotebookOpen[
FrontEndFindFileOnPath["paclet:guide/InstalledAddOns",
"PrivatePathsSystemResources"]]}]]


Note that "&AddOns Documentation..." can contain any string beginning with "&" that you would like. The Delimiter is also optional.

Part 2 can be accomplished by inserting the following after line 328.

,
Delimiter,


Obviously, you can set this color to whatever you want.

I recommend saving a copy of MenuSetup.tr` before doing this since you can break things by messing with it.

-

I modify my Menusetup.tr file a fair bit - mainly by adding extra menu items on the end.

"Note that "&AddOns Documentation..." can contain any string beginning with "&" that you would like......."

The & character is optional, and need not be the first character. It is not shown in the menu, but causes the next character to become an alt key mnemonic for accessing the menus from the keyboard.

Menusetup.tr contains a Mathematica expression, so it is absolutely vital to take a copy of the file before editing it. One misplaced { can render the whole thing unusable! Also, it seems to be a very restricted syntax - using operators like '+' messes it up - at least it did the last time I tried it.

I find that if you create a menu item that opens a box (such as InputString[]) it hangs the FE completely. This can be resolved by causing the menu item to initiate a delayed response using J/Link so that the menu code does not actually create the extra window. The delay can be much less than a second.

A very useful way to develop such menu extensions, is to add one new menu item that invokes Test[] (say). Then you can define the Test[] function from a notebook and try it out at once.

I do witsh Mathematica would add a decent way to set up menus programatically!

-