This is by no means a complete answer, or maybe no answer at all. I will show how to detect at least some BoxObjects in a displayed notebook, not how to construct them. Partly this was done already by @Silvia in this answer. She constructed some FrontEnd'NotebookInterfaceObject
s and I will show that they indeed agree with BoxObject
s. Then I will find some more BoxObject
s in a given notebook, with an embarassing result.
First a slight modification of the ingenious technique used by Silvia.
Start a fresh Mathematica session and evaluate the next command.
$PreRead=(Print[#];#)&;
Activate the debugger and evaluate
(1+2)/3+1
Deactivate the debugger and evaluate
Clear[$PreRead]
The printed box structure shows some DebugTag
s, of which the numbers correspond to FrontEnd'NotebookInterfaceObject
s, as shown by Silvia. Here I construct BoxObject
s with the same numbers:
Cases[ NotebookGet[SelectedNotebook[]] , {___,PatternSequence["DebugTag","[",n_,"]"],___}:>BoxObject[ToExpression[n]], Infinity]
A nice property of BoxObject
s is that when we click on the displayed number, the selection moves to the corresponding box structure. In this way we simply verify that these BoxObject
s coincide with the FrontEnd'NotebookInterfaceObject
s.
On my system, this way of finding BoxObject
s was not quite stable. Sometimes I found 4 boxes, sometimes 6.
Now I try to find more BoxObject
s in the notebook. For an existing BoxObject
, we can inspect the contents with NotebookRead
. If the box does not exist, the result will be $Failed
. In the following command, we look for all BoxObject
s in a certain range having non-empty contents. If we leave out the second condition in the If
statement, we find some empty BoxObject
s as well, whatever that may mean.
Table[With[{z=NotebookRead[BoxObject[n]]},
If[z=!=$Failed && z=!={}, BoxObject[n], Nothing]], {n, 1, 6000}]
Length[%]
(* list of displayed BoxObjects *)
(* 106 *)
The numbers of the found boxes all are 1 plus a multiple of 16:
Mod[%%[[All, 1]], 16] // Union
(* {1} *)
As already stated, clicking on a box number of a displayed BoxObject
moves the selection to the corresponding box. By doing so, we see that sometimes a complete cell is a BoxObject
, sometimes a single name or number is a BoxObject
. It also happens that when we click on a box number, Mathematica crashes.
My final remark is a little bit outside the question. A NotebookObject
or a CellObject
can be used to change the displayed properties by using kernel commands on these objects. So I tried to do the same with a BoxObject
:
SetOptions[BoxObject[2817], Background->Yellow]
Of course, the BoxObject
was choosen such that the box indeed had the option Background
. The effect of this command was that the background of the complete notebook became yellow. So I had to execute the above command once again, with Yellow
replaced with None
.
DebugTag
returned is the IDs of the Boxes in the Input line, while theDynamic[EvaluationBox]
gives the Box inside theDynamicBox
in the Output Cell. The IDs I think are in the some index sequence. $\endgroup$