13
$\begingroup$

I use some custom shortcut keys in KeyEventTranslations.tr. One is for the Delete All Output function:

Item[KeyEvent["w", Modifiers -> {Control}], 
    FrontEnd`FrontEndExecute[FrontEnd`FrontEndToken["DeleteGeneratedCells"]]]

or simply:

Item[KeyEvent["w", Modifiers -> {Control}], "DeleteGeneratedCells"]

This works as expected, putting up the dialog: "Do you really want to delete all the output cells in the notebook?". Is there any way to set up KeyEventTranslations.tr that when I hit Ctrl+w the dialog is automatically acknowledged and I don't have to hit Enter? The same goes for the Quit kernel function, that also puts up a dialog.

$\endgroup$
4
  • $\begingroup$ I have no idea how to answer this, but for reference I believe your code may be written: Item[KeyEvent["w", Modifiers -> {Control}], "DeleteGeneratedCells"] $\endgroup$
    – Mr.Wizard
    Commented Feb 19, 2012 at 12:22
  • $\begingroup$ @Mr.Wizard: How come FrontEndXxx functions are not necessary? $\endgroup$ Commented Feb 19, 2012 at 14:26
  • $\begingroup$ @István It's not completely clear to me what the difference is between something and FrontEnd`something. Maybe the only difference is that the latter form does not get evaluated in the kernel, but both of them might be evaluatable in the front end. I haven't dug deep into this. reference.wolfram.com/mathematica/tutorial/… $\endgroup$
    – Szabolcs
    Commented Feb 19, 2012 at 15:31
  • $\begingroup$ In this case I believe it is because the default object/action for an Item in KeyEventTranslations.tr is a FrontEnd token. As far as I know you do need the form you show if you are going to string several of them together, for example. $\endgroup$
    – Mr.Wizard
    Commented Feb 19, 2012 at 15:50

4 Answers 4

14
$\begingroup$

Try using this:

FrontEndExecute[
  {FrontEnd`NotebookFind[FrontEnd`SelectedNotebook[], 
                         "Output", All, CellStyle, AutoScroll->False], 
   FrontEnd`FrontEndToken["Clear"]}]

(Untested in KeyEventTranslations.tr, but works as a button!)


Regarding automating confirming the dialog---I don't think it is possible from within Mathematica. I'd like to note though that you can press Space to confirm the dialog (instead of using Enter), which is considerably easier for me due to the size and position of the key.


Update: As Albert Retey pointed out in a comment, this will only remove output cells, but not "Message" or "Print" cells. Those need to be added separately to the command, and this is still a workaround to finding all GeneratedCells.

$\endgroup$
5
  • 1
    $\begingroup$ Nice way to bypass the problem. I'll be interested to see if there is a solution for "EvaluatorQuit". $\endgroup$
    – Mr.Wizard
    Commented Feb 19, 2012 at 12:24
  • $\begingroup$ This method has the problem that it scrolls to the notebook end, unlike DeleteGeneratedCells. Maybe the position can be saved/restored? $\endgroup$
    – Meh
    Commented Feb 19, 2012 at 15:08
  • $\begingroup$ @Adal See if my edit fixes the issue. $\endgroup$
    – Szabolcs
    Commented Feb 19, 2012 at 15:11
  • $\begingroup$ I just wanted to mention that the above will not delete e.g. "Print" and "Message" cells, unlike the "DeleteGeneratedCells" token, which most probably will delete everything that has GeneratedCell -> True. Unfortunately I don't know how you could find these in one go with NotebookFind. Selecting every cell to check CurrentValue[NotebookSelection[],GeneratedCell] seem to be a lame (=very slow) alternative... $\endgroup$ Commented Feb 19, 2012 at 21:53
  • $\begingroup$ @Albert Good point. SelectionMove has a GeneratedCell unit, but it won't do what we need. I don't have any other solution than removing these three cell types one by one (and possibly missing others) $\endgroup$
    – Szabolcs
    Commented Feb 19, 2012 at 22:13
4
$\begingroup$

This just adds another hack for the Quit without confirm. It's not especially nice and I also haven't tested it in KeyEventTranslations.tr but it works from a Button with Evaluator -> None in versions 6,7 and 8 on Windows:

FrontEnd`FrontEndExecute[{
  FrontEnd`NotebookPut[
   Notebook[{
     Cell["NotebookClose[EvaluationNotebook[]];Quit[];", "Input"]
     },
    "ClosingSaveDialog" -> False,
    WindowSize -> {10, 10},
    WindowMargins -> {{-100, Automatic}, {-100, Automatic}}
    ]
   ],
  FrontEnd`FrontEndToken[FrontEnd`NotebookPutReturnObject[], 
   "EvaluateNotebook"],
  FrontEnd`SetSelectedNotebook[FrontEnd`InputNotebook[]]
  }]

it should be noted that Visible -> False for the Notebook does not work, thus the settings for WindowSize and WindowMargins. The SetSelectedNotebook seems to reset the focus to the button notebook when used from a button, but since none of these functions are documented I don't know how it will behave when used from a keyboard shortcut.

$\endgroup$
3
  • $\begingroup$ Works well in in KeyEventTranslations.tr (Mathematica 8.0.4 for Windows). Thank you. Really useful! $\endgroup$ Commented Feb 20, 2012 at 9:40
  • $\begingroup$ After some testing in real situations I found that it does not work if the keyboard combination is pressed when some long evaluation is in progress. User should stop evaluation in order to get the keyboard shortcut to work... :( $\endgroup$ Commented Feb 21, 2012 at 15:58
  • $\begingroup$ O.K., that is not unexpected: We are just adding a Quit to the evaluation queue. I think the most reasonable workaround would be to first abort (there is a shortcut for that, Alt-. on windows) and then after that quit. As I said, the above isn't well tested and might have other drawbacks, too. The best I'd hope would be that it saves some keystrokes for everyday usage... $\endgroup$ Commented Feb 23, 2012 at 8:22
2
$\begingroup$

Under version 10 at least for the Delete All Output menuoption one doesn't have to hit Enter any more to make it effective. This is not a full answer but it certainly makes my life one keystroke easier.

This now works without putting up a confirmation dialog:

FrontEndExecute@FrontEndToken@"DeleteGeneratedCells"

(Tested only under Win7 6-bit.)

$\endgroup$
0
$\begingroup$

An alternative is to customize the file MenuSetup.tr that is in the same folder of KeyEventTranslations.tr. The difference is that there you can use KernelEvaluate to execute a NotebookDelete. Here is an example of a new menu item. Insert this code in the list of Menu into the MenuSetup.tr file and restart Mathematica.

Menu["My commands", 
    {
     MenuItem["Delete All Output", KernelExecute[NotebookDelete[NotebookDelete[Cells[InputNotebook[], CellStyle -> "Output"]]]], MenuEvaluator -> "System", MenuKey["F10", Modifiers->{}]],
    MenuItem["Delete All Generated", KernelExecute[NotebookDelete[Cells[InputNotebook[], GeneratedCell -> True]]], MenuEvaluator -> "System", MenuKey["F", Modifiers->{}]]
    }]

The "Delete All Output" is already in the Cell menu and in M9 it pops the warning message up (in M10 it no longer did it).

$\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.