Reference Guides » Undocumented Contexts
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
FE`makePlainText
converts a front-end expression into plain text:
FE`makePlainText[NotebookRead[EvaluationCell[]]]
"In[6]:= FE`makePlainText[NotebookRead[EvaluationCell[]]]"
It's basically like FrontEndExecute
with ExportPacket
and "PlainText"
as described here .
Evaluate
FE`Evaluate
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 =
FE`Evaluate[
(* imports an Image as a GraphicsBox *)
FEPrivate`ImportImage@
FrontEnd`FileName[{"Ribbons", "Common"}, "Close.png"]
];
img // ToExpression (* creates the proper image from the GraphicsBox *)
Compare to:
RawBoxes@DynamicBox@
FEPrivate`ImportImage@
FrontEnd`FileName[{"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 FE`Evaluate
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 FrontEnd`SetValue
as a wrapper, e.g.:
MathLink`CallFrontEndHeld[FrontEnd`SetValue@FEPrivate`Set[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:
MathLink`CallFrontEndHeld[FrontEnd`SetValue@FEPrivate`Set[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:
MathLink`CallFrontEnd@
FrontEnd`SetValue@FEPrivate`Set[cache[EvaluationNotebook[]], 5]
And pull this with:
MathLink`CallFrontEnd@
FrontEnd`Value@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 FEPrivate`Set
and FEPrivate`SetDelayed
.