2.11.5 Advanced Topic: Executing Notebook Commands Directly in the Front End
When you execute a command like NotebookWrite[obj, data] the actual operation of inserting data into your notebook is performed in the front end. Normally, however, the kernel is needed in order to evaluate the original command, and to construct the appropriate request to send to the front end. But it turns out that the front end is set up to execute a limited collection of commands directly, without ever involving the kernel.
Distinguishing kernel and front end versions of commands.
The basic way that Mathematica distinguishes between commands to be executed in the kernel and to be executed directly in the front end is by using contexts. The kernel commands are in the usual System` context, but the front end commands are in the FrontEnd` context.
Sending an expression to be executed in the front end.
Here is a blank notebook.
This uses kernel commands to write data into the notebook.
In:= NotebookWrite[SelectedNotebook[ ], "x + y + z"]
In the kernel, these commands do absolutely nothing.
In:= FrontEnd`NotebookWrite[FrontEnd`SelectedNotebook[ ],
"a + b + c + d"]
If they are sent to the front end, however, they cause data to be written into the notebook.
If you write sophisticated programs for manipulating notebooks, then you will have no choice but to execute these programs primarily in the kernel. But for the kinds of operations typically performed by simple buttons, you may find that it is possible to execute all the commands you need directly in the front end—without the kernel even needing to be running.