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?
Casesat the proper level. See the third argument ofCases... use levelInfinity$\endgroup$Infinitymeans at calling at every level, yes? $\endgroup$Cases[{{Cell["foo", CellTags -> "bar"]}}, Cell[__, CellTags -> "bar", ___]]andCases[{Cell["foo", CellTags -> "bar"]}, Cell[__, CellTags -> "bar", ___]]... by defaultCasesoperates 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 useInfinityto tellCasesto look for the pattern at all levels. $\endgroup$Casesis set toInfinity. Read the question again. My question isn't why the 1st error is outputted, but why a perfectly validNotebookstructure causes the 2ndCasesto return a message in addition to returning the output. Something internally on howCasesis structure is causing the issue. $\endgroup$Casesto fail pastebin.com/uP7CNrUS $\endgroup$