The Mathematica front end supports a set of commands called front end tokens that allow you to perform actions such as opening or closing a notebook, that are typically carried out using menu items. Using front end tokens, you can control the behavior of the front end in an automated manner from the kernel. Front end tokens are, therefore, useful when writing programs to manipulate notebooks.
There is a front end token correpsonding to most common menu commands. For example, the "New" and "Paste" front end tokens correspond to the File New and Edit Paste menu commands, respectively. There are also a small number of front end tokens that do not correspond to a specific menu command.
You can call front end tokens by using a combination of the two commands FrontEndToken and FrontEndExecute. FrontEndToken identifies its argument as a front end token. FrontEndExecute is a kernel command that sends its argument to the front end for execution. For example, the following command tells the front end to create a new notebook.
Both FrontEndToken and FrontEndExecute accept a list as arguments, allowing you to execute multiple tokens in a single evaluation. In the following example, the Map function is used to apply FrontEndToken to a list of two front end tokens. When you evaluate this command, the front end first creates a new notebook and then pastes the contents of the clipboard into that notebook.
Types of Front End Tokens
Front end tokens are classified into two groups—simple tokens and compound tokens (also called tokens with parameters). Simple tokens perform a simple default action while compound tokens have variable parameters that can be used to modify some aspect of the token's behavior.
Simple tokens require FrontEndToken to have one or two arguments. If FrontEndToken has one argument, the token will operate on the current notebook. If FrontEndToken has two arguments, the first argument must be a NotebookObject, and the second the desired front end token.
We already saw an example using "New" in the Introduction. Here, we use "Save" to save the current notebook using FrontEndTokenExecute.
To save to some other notebook, you can use the two-argument form of FrontEndTokenExecute. In the two-argument form, the first argument is a NotebookObject corresponding to the notebook containing the current evaluation, and the second argument is the front end token.
Note: You can also execute a simple front end token by using the command FrontEndTokenExecute[token]. This is equivalent to FrontEndExecute[FrontEndToken[token]]. The shorter form is more convenient, but it does not allow you to execute compound tokens.
Compound tokens carry variable parameters that control some aspect of their behavior. Tokens of this type must be called using the three-argument form of FrontEndToken and then sent to the front end with FrontEndExecute. In the three-argument form of FrontEndToken, the first argument is the target notebook object, the second is the token, and the third is the value of the token parameter. For example, this command uses the token "SaveRenameSpecial" to save the selected notebook as plain text.
When using tokens that require parameters, you must always include the target notebook object as the first argument for FrontEndToken. If you do not specify the target notebook, you will get an error message.