How to execute kernel command from Front End?

   Item["Options &Explorer", KernelExecute[ToExpression["OptionsExplorer[]"]],


And I want to accomplish the same thing by a corresponding FrontEndAddMenuCommands... expression in an AutoloadPacletManagerConfigurationFrontEndinit.m.

But I don't find any KernelExecute function.

How to do it?

-
Isn't this just what you get when you type something in a cell in the front end, and hit Shift-Enter? So it is just telling the kernel to run the code inside its brackets. –  Verbeia Feb 20 '12 at 2:17
If I try to evaluate from a notebook input cell the FrontEndAddMenuCommands expression that includes that KernelExecute expression, it colors KernelExecute to indicate it's an unknown name. And trying the same thing from the indicated init.m seems to accomplish nothing -- but then perhaps I misunderstood what I need as the first argument to AddMenuCommands: I used "Input from Above", which is on the Insert menu. –  murray Feb 20 '12 at 2:29
See this related MathGroup posting. The function KernelExecute appears to be undocumented but exists and just tells the main kernel to run its argument. Try adding those commands into the file as instructed and see if it works. –  Verbeia Feb 20 '12 at 2:30
Rd @Verbeia's comment: the linked MathGroup posting by John Fultz concerns directly editing KeyEventTranslations.tr. But I'm not trying to do that; I want a way that makes changes only in $UserBaseDirectory, not $InstallationDirectory. Hence I'm trying to edit AutoloadPacletManagerConfigurationFrontEndinit.m. Moreover, I'm trying to insert an Item there that does two things: (1) create a new menu item (on the Insert menu); and (2) sets up a corresponding shortcut key -- as I showed in Kandrashkin's code snipped that appears in my question. –  murray Feb 20 '12 at 16:00
Here's what I added to the indicated init.m: FrontEndAddMenuCommands[ "Input from Above", {Delimiter, Item["Options &Explorer", KernelExecute[ToExpression["OptionsExplorer[]"]], MenuKey["o", Modifiers -> {Control, Command}], MenuEvaluator -> Automatic]}]; But after restart (with clean cache) nothing gets added to the Insert menu. Did I not properly identify the existing menu keyword in the 1st argument to AddMenuComands? Is something else wrong? –  murray Feb 20 '12 at 16:08

You can add the menu command to your Insert menu using AddMenuCommands in the following manner. (These modifications will only persist for the current front end session.)

First a demo function, just creating a dialog:

FrontEndExecute[FrontEndAddMenuCommands["DuplicatePreviousOutput",
FrontEndKernelExecute[CreateDialog[{TextCell["Click OK to close"],
DefaultButton[]}]],


(Note the required context specification for MenuEvaluator.)

This version will run OptionsExplorer[]

FrontEndExecute[FrontEndAddMenuCommands["DuplicatePreviousOutput",
SystemMenuEvaluator -> Automatic]}]]

Which init.m among the many in the tree? –  murray Feb 22 '12 at 20:10
Where's the corresponding init.m for Mac OS X to what @Chris Degnen gives in C:\Documents and Settings\Chris\Application Data\Mathematica\Kernel for Windows? –  murray Feb 23 '12 at 17:08
@murray evaluate this: FileNameJoin[{\$UserBaseDirectory, "Kernel"}] –  Mr.Wizard Feb 24 '12 at 22:22
@Chris Degnen: I've just tried this with version 9 and found that -- at least on Windows -- we need to add the FrontEnd context now for MenuKey and Modifiers`, too. Maybe you want add that to this answer? –  Albert Retey Dec 12 '12 at 12:57