36
$\begingroup$

What are some complete examples of what one would include in a FrontEnd init.m that would make use of FrontEnd`AddMenuCommands 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.

$\endgroup$
3
  • 1
    $\begingroup$ Is FrontEnd`AddMenuCommands 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 ... $\endgroup$
    – Spawn1701D
    Commented Jan 29, 2012 at 2:58
  • 1
    $\begingroup$ FrontEnd`AddMenuCommands 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. $\endgroup$
    – murray
    Commented Jan 29, 2012 at 4:54
  • $\begingroup$ I have not tried it myself but there is a resource function for this since 2020 (after this question) resources.wolframcloud.com/FunctionRepository/resources/… $\endgroup$ Commented Nov 24, 2022 at 22:08

3 Answers 3

26
$\begingroup$

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

FrontEndExecute[
 AddMenuCommands[
  "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]]}]]

Mathematica graphics

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

Further reading tells us:

You can completely reset the menus using...

FrontEndExecute[FrontEnd`ResetMenusPacket[{Automatic}]]
$\endgroup$
6
  • $\begingroup$ 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? $\endgroup$
    – murray
    Commented Jan 29, 2012 at 19:21
  • $\begingroup$ @murray good question; I'll play with this later today and see what I can determine. $\endgroup$
    – Mr.Wizard
    Commented Jan 29, 2012 at 21:15
  • 3
    $\begingroup$ 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 MathLink`CallFrontEnd (which you could also use) rather than LinkWrite to communicate with the FE. MathLink`CallFrontEnd 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. $\endgroup$
    – John Fultz
    Commented Feb 1, 2012 at 14:36
  • 3
    $\begingroup$ @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. $\endgroup$
    – John Fultz
    Commented Feb 1, 2012 at 14:45
  • $\begingroup$ @John about time an expert showed up; thanks! $\endgroup$
    – Mr.Wizard
    Commented Feb 1, 2012 at 16:53
16
$\begingroup$

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, 
 AddMenuCommands[
  "BackgroundDialog", {Delimiter, 
   Item["M&y Color", Background -> RGBColor[0.980, 0.941, 0.902]]}]]

To add a link to the AddOns documentation you can use...

LinkWrite[$ParentLink, 
 AddMenuCommands[
  "AboutBoxDialog", {Delimiter, 
   MenuItem["&AddOns Documentation...", 
    FrontEndExecute[{FrontEnd`NotebookOpen[
       FrontEnd`FindFileOnPath["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,
MenuItem["&AddOns Documentation...",
   FrontEndExecute[{FrontEnd`NotebookOpen[
     FrontEnd`FindFileOnPath["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,
  MenuItem["Custom",Background ->RGBColor[.5,.5,.5]]

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.

$\endgroup$
0
6
$\begingroup$

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!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.