6
$\begingroup$

Documentation for Yuri E. Kandrashkin's OptionsExplorer package says to add the following menu commands in MenuSetup.tr:

   Item["Options &Explorer", KernelExecute[ToExpression["OptionsExplorer[]"]],
   MenuKey["o", Modifiers->{Control,Command}], MenuEvaluator->Automatic]

And I want to accomplish the same thing by a corresponding FrontEnd`AddMenuCommands... expression in an Autoload`PacletManager`Configuration`FrontEnd`init.m.

But I don't find any KernelExecute function.

How to do it?

$\endgroup$
5
  • 1
    $\begingroup$ 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. $\endgroup$
    – Verbeia
    Commented Feb 20, 2012 at 2:17
  • $\begingroup$ If I try to evaluate from a notebook input cell the FrontEnd`AddMenuCommands 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. $\endgroup$
    – murray
    Commented Feb 20, 2012 at 2:29
  • $\begingroup$ 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. $\endgroup$
    – Verbeia
    Commented Feb 20, 2012 at 2:30
  • $\begingroup$ 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 Autoload`PacletManager`Configuration`FrontEnd`init.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. $\endgroup$
    – murray
    Commented Feb 20, 2012 at 16:00
  • $\begingroup$ Here's what I added to the indicated init.m: FrontEnd`AddMenuCommands[ "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? $\endgroup$
    – murray
    Commented Feb 20, 2012 at 16:08

1 Answer 1

7
$\begingroup$

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[FrontEnd`AddMenuCommands["DuplicatePreviousOutput",
  {Delimiter, MenuItem["CreateDialog &Demo",
    FrontEnd`KernelExecute[CreateDialog[{TextCell["Click OK to close"],
       DefaultButton[]}]], 
    MenuKey["D", Modifiers -> {"Control", "Shift"}],
    System`MenuEvaluator -> Automatic]}]]

(Note the required context specification for MenuEvaluator.)

This version will run OptionsExplorer[]

FrontEndExecute[FrontEnd`AddMenuCommands["DuplicatePreviousOutput",
  {Delimiter, MenuItem["Options &Explorer",
    FrontEnd`KernelExecute[ToExpression["OptionsExplorer[]"]],
    MenuKey["O", Modifiers -> {"Control", "Shift"}],
    System`MenuEvaluator -> Automatic]}]]
$\endgroup$
4
  • $\begingroup$ Which init.m among the many in the tree? $\endgroup$
    – murray
    Commented Feb 22, 2012 at 20:10
  • $\begingroup$ 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? $\endgroup$
    – murray
    Commented Feb 23, 2012 at 17:08
  • $\begingroup$ @murray evaluate this: FileNameJoin[{$UserBaseDirectory, "Kernel"}] $\endgroup$
    – Mr.Wizard
    Commented Feb 24, 2012 at 22:22
  • 1
    $\begingroup$ @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? $\endgroup$ Commented Dec 12, 2012 at 12:57

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.