# Ways to obtain a BoxObject

Is there any way to get a BoxObject, except with EvaluationBox or ParentBox?

So usually one needs to create objects with Initialization or Button and use EvaluationBox.

I would love to see more flexible approach, e.g., how to get a Box object from the current selection.

I was only able to find:

BoxObject expressions are typically generated using EvaluationBox.

which isn't much help.

The reason behind this question is that one can do interesting things with Cell or Box objects, the only problem is to get the required references.

• Could it be related to the  FrontEndNotebookInterfaceObject I mentioned in this post? Dec 16 '15 at 13:58
• Hmm... Anyway very interesting question! I wanted a generic box-object extractor for a long time... Dec 16 '15 at 14:10
• It looks like what the DebugTag returned is the IDs of the Boxes in the Input line, while the Dynamic[EvaluationBox] gives the Box inside the DynamicBox in the Output Cell. The IDs I think are in the some index sequence. Dec 16 '15 at 14:18
• ref/BoxObject is not one of the better efforts of WRI documentation staff, is it? Dec 16 '15 at 14:47

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'NotebookInterfaceObjects and I will show that they indeed agree with BoxObjects. Then I will find some more BoxObjects 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 DebugTags, of which the numbers correspond to FrontEnd'NotebookInterfaceObjects, as shown by Silvia. Here I construct BoxObjects with the same numbers:

Cases[ NotebookGet[SelectedNotebook[]] , {___,PatternSequence["DebugTag","[",n_,"]"],___}:>BoxObject[ToExpression[n]], Infinity]


A nice property of BoxObjects 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 BoxObjects coincide with the FrontEnd'NotebookInterfaceObjects.

On my system, this way of finding BoxObjects was not quite stable. Sometimes I found 4 boxes, sometimes 6.

Now I try to find more BoxObjects 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 BoxObjects in a certain range having non-empty contents. If we leave out the second condition in the If statement, we find some empty BoxObjects 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.

• Interesting finding about the Mod[..., 16] one! +1 Dec 18 '15 at 8:20
• Don't write any production code based upon any of this. If you use anything other than ParentBox or EvaluationBox`, expect that, in some future version, your code will break. Jun 26 '18 at 19:17