2
$\begingroup$

I'm trying to create Menu Command that selects all cells in a notebook and closes them. I am able to select all the cells but cannot close them. Below is my attempt I placed in an init.m file.

FrontEndExecute[
  FrontEnd`AddMenuCommands[
   "DuplicatePreviousOutput",
   {MenuItem[
     "Close All Groups", {FrontEnd`KernelExecute[
       SelectionMove[InputNotebook[], All, Notebook]];
      FrontEnd`KernelExecute[
       FrontEndToken[InputNotebook[], "SelectionCloseAllGroups"]]},
     FrontEnd`MenuKey["c", FrontEnd`Modifiers -> {"Control"}],
     System`MenuEvaluator -> Automatic
    ]}
  ]
];

I believe my problem is using the FrontEndToken in the init.m file. I'm not sure how to use them in this case.

$\endgroup$
5
$\begingroup$

Try this instead...

FrontEndExecute@FrontEnd`AddMenuCommands["DuplicatePreviousOutput",
  {
   MenuItem["Close All Groups",
    FrontEnd`KernelExecute[
      SelectionMove[InputNotebook[], All, Notebook];
      FrontEndTokenExecute[InputNotebook[],"SelectionCloseAllGroups"]
    ], 
    FrontEnd`MenuKey["c", FrontEnd`Modifiers -> {"Control", "Shift"}],
    System`MenuEvaluator ->Automatic]
  }
]

I've moved the keyboard accelerator to Control + Shift, mainly for the benefit of Windows and Linux users.

$\endgroup$
  • $\begingroup$ This worked. This should help me in the future with frontenstokens in unit.m files. I get lost sometimes with namespaces. $\endgroup$ – Michael McCain Mar 12 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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