Note: Spelunking will likely be needed.
Execute the following. You should get back and error along the lines of First::first:..
. This error is unimportant except it helps illustrate a potential issue with Cases
internal code.
Print[Button["Print Cell",
Cell[BoxData[
ToBoxes@
First@Cases[NotebookGet[EvaluationNotebook[]][[1]],
Cell[___, CellTags -> "MyCode", ___]]
], "Input", CellTags -> "MyGraphic"]
]
];
CellPrint@
Cell[BoxData[
ToBoxes[Cases[NotebookGet[EvaluationNotebook[]],
Cell[___, CellTags -> "MyGraphic", ___], Infinity], StandardForm]
], "Output", CellTags -> "MyCode"];
Now ultimately although the notebook is outputting an error the Notebook itself should have a valid structure, considering the notebook can be saved and duplicated like so NotebookPut@NotebookGet[EvaluationNotebook[]]
.
Interestingly if execute the following code in the same Notebook
.
Cases[NotebookGet[EvaluationNotebook[]]
, Cell[__, CellTags -> "MyGraphic"], Infinity]
Cases
return the appropriate values and you get back a First::first
error.
My question: Why does Cases
return a message from a perfectly valid Notebook
structure. Other then using Quiet
to suppress an error, how might I solve such a problem?
Cases
at the proper level. See the third argument ofCases
... use levelInfinity
$\endgroup$ – rm -rf♦ Aug 8 '13 at 18:33Infinity
means at calling at every level, yes? $\endgroup$ – William Aug 8 '13 at 18:35Cases[{{Cell["foo", CellTags -> "bar"]}}, Cell[__, CellTags -> "bar", ___]]
andCases[{Cell["foo", CellTags -> "bar"]}, Cell[__, CellTags -> "bar", ___]]
... by defaultCases
operates at level spec{1}
, which is why the first returns{}
whereas the second returns a cell. In a generic notebook expression, the cell tag rule is at an arbitrary depth, which you may or may not know in advance. It certainly isn't at level{1}
. So you useInfinity
to tellCases
to look for the pattern at all levels. $\endgroup$ – rm -rf♦ Aug 8 '13 at 18:40Cases
is set toInfinity
. Read the question again. My question isn't why the 1st error is outputted, but why a perfectly validNotebook
structure causes the 2ndCases
to return a message in addition to returning the output. Something internally on howCases
is structure is causing the issue. $\endgroup$ – William Aug 8 '13 at 18:43Cases
to fail pastebin.com/uP7CNrUS $\endgroup$ – William Aug 8 '13 at 18:47