While playing around with $Pre
and $Post
I created this very annoying piece of code:
$Post = If[# =!=
Null, (Print@"Are you sure you want to know the result?";
Print@Row@{Button["Yes", Print@#], Button["No", Null]}), Null] &;
I have not yet found a use for it (other than possibly playing pranks on Mma newbies) but it led me to the following question:
How can one delete a certain cell that was created by Print
(i.e. without selecting the cell manually)? And furthermore, how can one create a button that deletes itself upon being clicked (still evaluating the action)?
Ultimately I want the button to delete both itself and the "Are you sure..."
cell.
I found out in the documentation that PrintTemporary
objects can be deleted using NotebookDelete
. Is there a similar way for Print
cells? I think something similar to what I want could be done using ChoiceDialog
and the like, but I'm really interested in deleting the Print
cells.
Answer
A self-destructing cell that creates a self-destructing button which deletes all cells generated by Print
:
(credit: Sasha, jVincent and Yves Klett for the ideas in answers/comments in the linked Q/As)
Print[Button["Delete Print-generated cells & disappear",
NotebookFind[SelectedNotebook[], "Print", All, CellStyle]; NotebookDelete[]]];
SelectionMove[EvaluationNotebook[], All, EvaluationCell]; NotebookDelete[];
After evaluating the cell
NotebookFind[SelectedNotebook[], "Print", All, CellStyle];
Note the CellStyles
(highlighted) of an ordinary Output
cell and that of the one produced by Print
.
After evaluating
Print[Button["Delete Print-generated cells & disappear",
NotebookFind[SelectedNotebook[], "Print", All, CellStyle]; NotebookDelete[]]];
SelectionMove[EvaluationNotebook[], All, EvaluationCell]; NotebookDelete[];
After the clicking the button:
Comments
Post a Comment