# How to automate a FrontEnd return?

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

Item[KeyEvent["w", Modifiers -> {Control}],
FrontEndFrontEndExecute[FrontEndFrontEndToken["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.

-
I have no idea how to answer this, but for reference I believe your code may be written: Item[KeyEvent["w", Modifiers -> {Control}], "DeleteGeneratedCells"] –  Mr.Wizard Feb 19 '12 at 12:22
@Mr.Wizard: How come FrontEndXxx functions are not necessary? –  István Zachar Feb 19 '12 at 14:26
@István It's not completely clear to me what the difference is between something and FrontEndsomething. 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/… –  Szabolcs Feb 19 '12 at 15:31
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. –  Mr.Wizard Feb 19 '12 at 15:50

Try using this:

FrontEndExecute[
{FrontEndNotebookFind[FrontEndSelectedNotebook[],
"Output", All, CellStyle, AutoScroll->False],
FrontEndFrontEndToken["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.

-
Nice way to bypass the problem. I'll be interested to see if there is a solution for "EvaluatorQuit". –  Mr.Wizard Feb 19 '12 at 12:24
This method has the problem that it scrolls to the notebook end, unlike DeleteGeneratedCells. Maybe the position can be saved/restored? –  Adal Feb 19 '12 at 15:08
@Adal See if my edit fixes the issue. –  Szabolcs Feb 19 '12 at 15:11
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... –  Albert Retey Feb 19 '12 at 21:53
@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) –  Szabolcs Feb 19 '12 at 22:13

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:

FrontEndFrontEndExecute[{
FrontEndNotebookPut[
Notebook[{
Cell["NotebookClose[EvaluationNotebook[]];Quit[];", "Input"]
},
"ClosingSaveDialog" -> False,
WindowSize -> {10, 10},
WindowMargins -> {{-100, Automatic}, {-100, Automatic}}
]
],
FrontEndFrontEndToken[FrontEndNotebookPutReturnObject[],
"EvaluateNotebook"],
FrontEndSetSelectedNotebook[FrontEndInputNotebook[]]
}]


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.

-
Works well in in KeyEventTranslations.tr (Mathematica 8.0.4 for Windows). Thank you. Really useful! –  Alexey Popkov Feb 20 '12 at 9:40
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... :( –  Alexey Popkov Feb 21 '12 at 15:58
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... –  Albert Retey Feb 23 '12 at 8:22

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.)

-

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",
{