9
$\begingroup$

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.

$\endgroup$
4
  • 2
    $\begingroup$ Could it be related to the `` FrontEnd`NotebookInterfaceObject`` I mentioned in this post? $\endgroup$
    – Silvia
    Commented Dec 16, 2015 at 13:58
  • 1
    $\begingroup$ Hmm... Anyway very interesting question! I wanted a generic box-object extractor for a long time... $\endgroup$
    – Silvia
    Commented Dec 16, 2015 at 14:10
  • 1
    $\begingroup$ 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. $\endgroup$
    – Silvia
    Commented Dec 16, 2015 at 14:18
  • 4
    $\begingroup$ ref/BoxObject is not one of the better efforts of WRI documentation staff, is it? $\endgroup$
    – m_goldberg
    Commented Dec 16, 2015 at 14:47

1 Answer 1

3
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Interesting finding about the Mod[..., 16] one! +1 $\endgroup$
    – Silvia
    Commented Dec 18, 2015 at 8:20
  • 4
    $\begingroup$ 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. $\endgroup$
    – John Fultz
    Commented Jun 26, 2018 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.