I'm trying to have a custom function evaluated via a keyboard shortcut, and add this shortcut via the KeyEventTranslations.tr.
As a test example I took the example provided in this answer to add and evaluate the expression x=1 when pressing ctrl+u.
My only problem with that is that it leaves the x=1 expression on the notebook, while I don't want to see it.
I therefore tried to delete it after evaluation, using the following:
Item[KeyEvent["u", Modifiers -> {Control}],
FrontEndExecute[
FrontEnd`SelectionMove[FrontEnd`InputNotebook[], After, Cell];
FrontEnd`NotebookWrite[
FrontEnd`InputNotebook[],
Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]
];
FrontEnd`SelectionMove[FrontEnd`InputNotebook[], Previous, Cell];
FrontEnd`SelectionEvaluate[FrontEnd`InputNotebook[]];
FrontEnd`SelectionMove[FrontEnd`InputNotebook[], Previous, CellGroup, 1];
FrontEnd`NotebookDelete[FrontEnd`InputNotebook[]]
]
]
This is however not working for some reason.
It seems to work up to the delete line (even though I didn't manage to properly select the whole cell group).
When the last NotebookDelete line is added however, nothing seems to happen: the value of x is not changed at all and no expression is printer.
How can I modify this to have this toy expression (or any other expression) work in this way?
Of course, a way to just evaluate the expression without writing it in the notebook at all would be also appreciated!
KernelExecute. See for example what Rolf Mertig does here. If you need to work with a notebook but don't want it visible useFrontEnd`NotebookSuspendScreenUpdateslike I do here – b3m2a1 Aug 27 '17 at 19:14FrontEndExecutetakes a list for multiple commands as it's notHold*so that should have all evaluated out toFrontEnd`NotebookDelete[FrontEnd`InputNotebook[]]before being passed toFrontEndExecute. – b3m2a1 Aug 27 '17 at 19:25NotebookWrite. That cuts out one selection move, so that's nice. – b3m2a1 Aug 27 '17 at 19:33