## Front End Programming

The Mathematica front-end is almost as complex as the Mathematica kernel, but much worse understood. There are a number of contexts which implement different functionality that it uses.

## FrontEnd

The "FrontEnd" context supplies things like "front-end packets" which tell the front-end to do things.

There is a decent listing of these with usages here .

## FE

The "FE" context provides utilities used by the front-end

### makePlainText

FEmakePlainText converts a front-end expression into plain text:

 FEmakePlainText[NotebookRead[EvaluationCell[]]]

 "In[6]:= FEmakePlainText[NotebookRead[EvaluationCell[]]]"


It's basically like FrontEndExecute with ExportPacket and "PlainText" as described here .

### Evaluate

FEEvaluate evaluates front-end expressions, basically. It'll usually imitate what you see when working with these things in the actual front-end. E.g.:

 img =
FEEvaluate[
(* imports an Image as a GraphicsBox *)

FEPrivateImportImage@
FrontEndFileName[{"Ribbons", "Common"}, "Close.png"]
];
img // ToExpression (* creates the proper image from the GraphicsBox *)


Compare to:

 RawBoxes@DynamicBox@
FEPrivateImportImage@
FrontEndFileName[{"Ribbons", "Common"}, "Close.png"]


## FEPrivate

The "FEPrivate" context supplies a mini-language that the front-end understands. Here are some of it's useful structures.

The language itself is much more rigid and inflexible, given that it's closer to a DSL than a standard language.

 Select[
StringSplit[Names["FEPrivate*"], ""][[All, -1]],
NameQ
]

 {"And","EmitSound","FrontEndExecute","FrontEndResource","FrontEndResourceString","Greater","Head","If","Less","MemberQ","Not","Or","Part","SameQ","Set","SetDelayed","Switch","UnsameQ","Which","$ActivationKey","$OperatingSystem","\$SystemID"}


### If / Switch / Which / And / Or / Not / MemberQ / SameQ / Head / Greater / Less / Part / Apply / Etc.

These are basically the same as in the kernel, but obviously do not evaluate unless called via FEEvaluate on in the front-end itself. One thing to note is that many constructs are less powerful than in the kernel, so stick to simple things when possible.

### Set / SetDelayed

These are used to represent Set and SetDelayed , as one would expect, but they're almost always used with FrontEndSetValue as a wrapper, e.g.:

 MathLinkCallFrontEndHeld[FrontEndSetValue@FEPrivateSet[myVar, 2]]


Now the FE remembers that and we can get that value back from it later, maybe in a DynamicBox :

 RawBoxes@DynamicBox[myVar]


Note that myVar still has no value:

 myVar

 myVar


And if we change that value of myVar in the FE the Dynamic value will also update:

 MathLinkCallFrontEndHeld[FrontEndSetValue@FEPrivateSet[myVar, 5]]
(* now the prior DynamicBox will read 5 *)


This can be useful as a way to set state. We can also use slightly more complicated patterns like:

 MathLinkCallFrontEnd@
FrontEndSetValue@FEPrivateSet[cache[EvaluationNotebook[]], 5]


And pull this with:

 MathLinkCallFrontEnd@
FrontEndValue@cache[EvaluationNotebook[]]

 5


Note, though, that this type of thing can't be used with DynamicBox , though

 RawBoxes@DynamicBox[cache[EvaluationNotebook[]]]


The most common use of Set and SetDelayed , though, is in the CurrentValue mechanism. The way CurrentValue updates is through FEPrivateSet and FEPrivateSetDelayed` .