Function Repository Resource:

GetBoxPositions

Source Notebook

Get positions of all boxes in a notebook

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["GetBoxPositions"][]

gets positions of boxes in the current notebook.

ResourceFunction["GetBoxPositions"][nb]

gets positions in the specified notebook.

ResourceFunction["GetBoxPositions"][nb,type]

gets only boxes matching the given pattern.

Details and Options

Positions are in absolute screen coordinates.
ResourceFunction["GetBoxPositions"][nb] is equivalent to ResourceFunction["GetBoxPositions"][nb,_].

Examples

Basic Examples (3) 

Get positions of boxes in the current notebook:

In[1]:=
ResourceFunction["GetBoxPositions"][] // Short
Out[1]=

Get box positions for another notebook:

In[2]:=
nb = CreateDocument[{Style["This is a style box", Red], Button["This is a button box", Null]}]
Out[2]=
In[3]:=
ResourceFunction["GetBoxPositions"][nb]
Out[3]=

Find specific box types:

In[4]:=
ResourceFunction["GetBoxPositions"][nb, "ButtonBox" | "StyleBox"]
Out[4]=
In[5]:=
NotebookClose[nb]

Applications (1) 

Find buttons in another notebook and click them automatically:

In[6]:=
color[___] := Automatic;
nb = CreateDocument[{Grid[
    Array[Button["Click Me", color[##] = RandomColor[], Background -> Dynamic[color[##]]] &, {3, 3}]], Button["Last One", Print["Good robot!"]]}]
Out[5]=
In[7]:=
positions = First[ResourceFunction["GetBoxPositions"][nb, "ButtonBox"]]
Out[7]=
In[8]:=
SetSelectedNotebook[nb];
ResourceFunction["MoveMouse"][All, #, "Click"] & /@ positions
Out[9]=

Version History

  • 1.0.0 – 27 May 2020

Related Resources

License Information