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– rm -rf Aug 08 '13 at 18:33Infinitymeans at calling at every level, yes? – William Aug 08 '13 at 18:35Cases[{{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. – rm -rf Aug 08 '13 at 18:40Casesis 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. – William Aug 08 '13 at 18:43Casesto fail http://pastebin.com/uP7CNrUS – William Aug 08 '13 at 18:47Casesevaluates the arguments. Your button is what's causing it to throw an error. While the rest of the notebook is a bunch of inert strings in boxes, the button function is present as an evaluatable expression. WhenCasesencounters it, it evaluates it and this expression isFirst@Cases[...], which as I explained earlier, should throw an error. – rm -rf Aug 08 '13 at 18:50Caseswas evaluating something ;). Might you know of a fix the prevents it from evaluating the expressions and therefore allows the code to work properly. – William Aug 08 '13 at 19:02Casesthat doesn't evaluate its leaves, that's a bigger task which possibly deserves its own question. I don't know how to write such a version ofCasesoff the top of my head, but you should read this answer by Leonid. You might be able to use it to "shield" certain expressions (likeButtonFunction's RHS, for example). – rm -rf Aug 08 '13 at 19:08