Re: How add a menu item with a menu key using an init.m
- To: mathgroup at smc.vnet.net
- Subject: [mg125305] Re: How add a menu item with a menu key using an init.m
- From: John Fultz <jfultz at wolfram.com>
- Date: Mon, 5 Mar 2012 02:42:37 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Reply-to: jfultz at wolfram.com
On Sun, 4 Mar 2012 04:32:46 -0500 (EST), Chris Degnen wrote:
> On Thursday, March 1, 2012 10:39:47 AM UTC, John Fultz wrote:
>> On Sat, 25 Feb 2012 01:58:51 -0500 (EST), Chris Degnen wrote:
>>> Murray Eisenberg wrote:
>>>
>>>> Here's what I put there:
>>>>
>>>> FrontEnd`AddMenuCommands["Input from Above",
>>>> {Delimiter,Item["Options &Explorer",
>>>> KernelExecute[ToExpression["OptionsExplorer[]"]],
>>>> MenuKey["o",Modifiers->{Control,Command}],
>>>> MenuEvaluator->Automatic]}];
>>>>
>>>> But nothing happens after a restart of Mathematica with clean cache.
>>>> What's wrong? Did I err in indicating the Insert menu's entry "Item
>>>> from
>>>> Above" referenced there? Or something else.
>>>>
>>> There are two problems: the front end token for the Insert
>>> menu position would be "DuplicatePreviousOutput", and, more
>>> problematically, AddMenuCommands can only add simple
>>> front end tokens like "OptionsDialog" (as demonstrated by
>>> Andy Ross).
>>>
>>> FrontEndExecute[AddMenuCommands["DuplicatePreviousOutput",
>>> {Delimiter, MenuItem["Options &Explorer", "OptionsDialog",
>>> MenuKey["O", Modifiers -> {"Control", "Shift"}]]}]];
>>>
>> No, you have this backwards. AddMenuCommands can only *position* a menu
>> relative to one with a simple front end token. The command that you're
>> adding
>> can be any menu command.
>>
> Thanks for the clarification, but in that case should KernelExecute
> menu items also work?
>
> I have made three examples to illustrate the question.
>
> (For readers, these menu modifications only persist for the current
> front end session.)
>
> The first one adds a MathGroup link to the Help menu, and works
> fine, (at least with Chrome).
>
> FrontEndExecute[
> AddMenuCommands[
> "SelectionHelpDialog", {Delimiter,
> MenuItem["&MathGroup Archive",
> FrontEndExecute[{FrontEnd`NotebookLocate[{URL[
> "http://forums.wolfram.com/mathgroup"], None}]}],
> MenuKey["M", Modifiers -> {"Control", "Shift"}]]}]]
>
>
> The second example adds a custom function at the same location on
> the Help menu. The MenuItem works if it is included in MenuSetup.tr
> but when added through AddMenuCommands it produces the message
> "Could not process unknown packet "CreateDialog"."
>
> FrontEndExecute[
> AddMenuCommands[
> "SelectionHelpDialog", {Delimiter,
> MenuItem["Custom Function version 1",
> KernelExecute[
> CreateDialog[{TextCell["Click OK to close"],
> DefaultButton[]}]],
> MenuEvaluator -> Automatic]}]]
>
>
> The third example is a roundabout way to achieve what the second
> example attempts. It works for the function demonstrated, but it
> would not work for OptionsExplorer[] which need to access the
> current front end selection. (OptionsExplorer seems to have some
> obsolescence problems, by the way, before anyone spends time.)
>
>
> FrontEndExecute[
> AddMenuCommands[
> "SelectionHelpDialog", {Delimiter,
> MenuItem["Custom Function version 2",
> FrontEndExecute[
> {FrontEnd`NotebookWrite[FrontEnd`NotebookCreate[],
> RowBox[{RowBox[{"CreateDialog", "[", RowBox[{"{",
> RowBox[
> {RowBox[
> {"TextCell",
> "[", "\"\<Click OK to close\>\"", "]"}], ",",
> RowBox[{"DefaultButton", "[", "]"}]}],
> "}"}], "]"}], ";",
> RowBox[{"NotebookClose", "[", "]"}]}]],
> FrontEndToken["EvaluateCells"]}],
> MenuKey["F", Modifiers -> {"Control", "Shift"}]]}]]
>
> Is there some way to run KernelExecute with AddMenuCommands?
>
> Thanks, Chris
When the FE reads MwenuSetup.tr, it assumes all of the symbols are in the
System` context unless specified otherwise. But some of the symbols, including
those which usefully control front end functionality, haven't been added to the
System` context in the kernel. The expression then gets parsed by the kernel
using, typically, the Global` context, and the FE gets muddled when it read, for
example, Global`MenuEvaluator.
This isn't too hard to work around. Your second expression, for example, works
fine if you do:
FrontEndExecute[
FrontEnd`AddMenuCommands[
"SelectionHelpDialog", {Delimiter,
MenuItem["Custom Function version 1",
FrontEnd`KernelExecute[
CreateDialog[{TextCell["Click OK to close"], DefaultButton[]}]],
System`MenuEvaluator -> Automatic]}]]
Note that the addition of the FrontEnd` contexts isn't strictly required...this
is a bit of pedanticism on my part that I think makes a better guarantee of
forward compatibility for front end packets. But the System`MenuEvaluator
absolutely is required. Yes, it probably ought to be added to the System`
context for real in a future version, but that's what you've got to do for now.
Sincerely,
John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.