Skip to main content
added 936 characters in body
Source Link
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375

According to the Documentation for ErrorBox,

screenshot

In our case the ErrorBox is implicit, i.e. is generated by the FrontEnd upon rendering, not by the kernel. Unfortunately, the Documentation is silent about how to detect such an implicit ErrorBox.

On the other hand, the presence of such an implicit ErrorBox has several consequences:

  1. It displays the contents of the cell in red.
  2. It displays the cell bracket in red.
  3. It displays the tooltip when the mouse is over the contents of the cell.
  4. It displays the banner near to the cell bracket.
  5. It prints a message to the MessagesNotebook[] when one clicks on the banner.

We can try to find out the presence of an implicit ErrorBox by checking the above features using the built-in "RobotTools`" package:

Needs["RobotTools`"]

Note that below I will use Mathematica 12.3.1, because in versions 13.0 - 13.1 this package is partially broken due to the hidden scaling (which can be turned off, however).

Let us try to click on the banner near to the cell bracket.

First, create a new Notebook window where the relative position of the banner is predictable:

nb = CreateDocument[ExpressionCell[Image[RandomImage[10], ImageSize -> x]]]

Now get the coordinates of the top right corner of the new Notebook:

topRightCorner = Extract[GetWindowRectangle[nb], {{2, 1}, {1, 2}}]
{1566., 219.}

The banner is located just below and to the left of the upper right corner:

screenshot

Let's click on it and check what is printed to the MessagesNotebook[]:

before = First@NotebookGet[MessagesNotebook[]];
MouseClick[nb, topRightCorner + {-32, 4}]
new = Complement[First@NotebookGet[MessagesNotebook[]], before];
If[Length[new] > 0, CellPrint /@ new];

out

The messages are printed, what means that we've specified the coordinates correctly.


Similar approach can be based on CurrentNotebookImage (which in turn is based on "RobotTools`", and hence is broken in versions 13.0 - 13.1). We create a new frameless Notebook window which has no WindowElements and contains only one closed cell, and take a screenshot of it. Since the contents of the cell isn't shown, we get only the insertion point and the error banner:

nb = CreateDocument[
   ExpressionCell[Image[RandomImage[10], ImageSize -> x], 
    CellOpen -> False], WindowElements -> {}, 
   WindowFrameElements -> {}, WindowFrame -> "Frameless", 
   WindowSize -> {100, 30}];
Pause[.1]
img = CurrentNotebookImage[nb]
NotebookClose[nb]

out

Now it is sufficient to check whether the screenshot contains the color {255, 98, 98}:

FreeQ[ImageData[img, Byte], {255, 98, 98}]
False

According to the Documentation for ErrorBox,

screenshot

In our case the ErrorBox is implicit, i.e. is generated by the FrontEnd upon rendering, not by the kernel. Unfortunately, the Documentation is silent about how to detect such an implicit ErrorBox.

On the other hand, the presence of such an implicit ErrorBox has several consequences:

  1. It displays the contents of the cell in red.
  2. It displays the cell bracket in red.
  3. It displays the tooltip when the mouse is over the contents of the cell.
  4. It displays the banner near to the cell bracket.
  5. It prints a message to the MessagesNotebook[] when one clicks on the banner.

We can try to find out the presence of an implicit ErrorBox by checking the above features using the built-in "RobotTools`" package:

Needs["RobotTools`"]

Note that below I will use Mathematica 12.3.1, because in versions 13.0 - 13.1 this package is partially broken due to the hidden scaling (which can be turned off, however).

Let us try to click on the banner near to the cell bracket.

First, create a new Notebook window where the relative position of the banner is predictable:

nb = CreateDocument[ExpressionCell[Image[RandomImage[10], ImageSize -> x]]]

Now get the coordinates of the top right corner of the new Notebook:

topRightCorner = Extract[GetWindowRectangle[nb], {{2, 1}, {1, 2}}]
{1566., 219.}

The banner is located just below and to the left of the upper right corner:

screenshot

Let's click on it and check what is printed to the MessagesNotebook[]:

before = First@NotebookGet[MessagesNotebook[]];
MouseClick[nb, topRightCorner + {-32, 4}]
new = Complement[First@NotebookGet[MessagesNotebook[]], before];
If[Length[new] > 0, CellPrint /@ new];

out

The messages are printed, what means that we've specified the coordinates correctly.

According to the Documentation for ErrorBox,

screenshot

In our case the ErrorBox is implicit, i.e. is generated by the FrontEnd upon rendering, not by the kernel. Unfortunately, the Documentation is silent about how to detect such an implicit ErrorBox.

On the other hand, the presence of such an implicit ErrorBox has several consequences:

  1. It displays the contents of the cell in red.
  2. It displays the cell bracket in red.
  3. It displays the tooltip when the mouse is over the contents of the cell.
  4. It displays the banner near to the cell bracket.
  5. It prints a message to the MessagesNotebook[] when one clicks on the banner.

We can try to find out the presence of an implicit ErrorBox by checking the above features using the built-in "RobotTools`" package:

Needs["RobotTools`"]

Note that below I will use Mathematica 12.3.1, because in versions 13.0 - 13.1 this package is partially broken due to the hidden scaling (which can be turned off, however).

Let us try to click on the banner near to the cell bracket.

First, create a new Notebook window where the relative position of the banner is predictable:

nb = CreateDocument[ExpressionCell[Image[RandomImage[10], ImageSize -> x]]]

Now get the coordinates of the top right corner of the new Notebook:

topRightCorner = Extract[GetWindowRectangle[nb], {{2, 1}, {1, 2}}]
{1566., 219.}

The banner is located just below and to the left of the upper right corner:

screenshot

Let's click on it and check what is printed to the MessagesNotebook[]:

before = First@NotebookGet[MessagesNotebook[]];
MouseClick[nb, topRightCorner + {-32, 4}]
new = Complement[First@NotebookGet[MessagesNotebook[]], before];
If[Length[new] > 0, CellPrint /@ new];

out

The messages are printed, what means that we've specified the coordinates correctly.


Similar approach can be based on CurrentNotebookImage (which in turn is based on "RobotTools`", and hence is broken in versions 13.0 - 13.1). We create a new frameless Notebook window which has no WindowElements and contains only one closed cell, and take a screenshot of it. Since the contents of the cell isn't shown, we get only the insertion point and the error banner:

nb = CreateDocument[
   ExpressionCell[Image[RandomImage[10], ImageSize -> x], 
    CellOpen -> False], WindowElements -> {}, 
   WindowFrameElements -> {}, WindowFrame -> "Frameless", 
   WindowSize -> {100, 30}];
Pause[.1]
img = CurrentNotebookImage[nb]
NotebookClose[nb]

out

Now it is sufficient to check whether the screenshot contains the color {255, 98, 98}:

FreeQ[ImageData[img, Byte], {255, 98, 98}]
False
Source Link
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375

According to the Documentation for ErrorBox,

screenshot

In our case the ErrorBox is implicit, i.e. is generated by the FrontEnd upon rendering, not by the kernel. Unfortunately, the Documentation is silent about how to detect such an implicit ErrorBox.

On the other hand, the presence of such an implicit ErrorBox has several consequences:

  1. It displays the contents of the cell in red.
  2. It displays the cell bracket in red.
  3. It displays the tooltip when the mouse is over the contents of the cell.
  4. It displays the banner near to the cell bracket.
  5. It prints a message to the MessagesNotebook[] when one clicks on the banner.

We can try to find out the presence of an implicit ErrorBox by checking the above features using the built-in "RobotTools`" package:

Needs["RobotTools`"]

Note that below I will use Mathematica 12.3.1, because in versions 13.0 - 13.1 this package is partially broken due to the hidden scaling (which can be turned off, however).

Let us try to click on the banner near to the cell bracket.

First, create a new Notebook window where the relative position of the banner is predictable:

nb = CreateDocument[ExpressionCell[Image[RandomImage[10], ImageSize -> x]]]

Now get the coordinates of the top right corner of the new Notebook:

topRightCorner = Extract[GetWindowRectangle[nb], {{2, 1}, {1, 2}}]
{1566., 219.}

The banner is located just below and to the left of the upper right corner:

screenshot

Let's click on it and check what is printed to the MessagesNotebook[]:

before = First@NotebookGet[MessagesNotebook[]];
MouseClick[nb, topRightCorner + {-32, 4}]
new = Complement[First@NotebookGet[MessagesNotebook[]], before];
If[Length[new] > 0, CellPrint /@ new];

out

The messages are printed, what means that we've specified the coordinates correctly.