6

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.

einbandi
  • 4,024
  • 1
  • 23
  • 39
  • 1
    To select "Print" cells, use something like the programmatic approaches here or here (modify to select a print cell). As for the button question, I'm certain it was asked previously (either here or on SO) and answered either by Heike or Sjoerd (if memory serves me right). I need to leave now, but perhaps this might be enough to get started (or searching) – rm -rf Jan 18 '13 at 00:05
  • I thought someone must have asked this, but I could't find anything. Thanks for the links. I'll look into them in detail tomorrow. It seems like tagging the cells as in the second link should work for me. – einbandi Jan 18 '13 at 00:16
  • 2
    @rm-rf. Regarding the button question: were you thinking of this – m_goldberg Jan 18 '13 at 00:23
  • @m_goldberg That seems to be the one (I must've misremembered the users). There's also this very relevant question on self-destructing buttons and this one on self-destructing input cells, both of which can possibly be modified for the question here. – rm -rf Jan 18 '13 at 01:10

2 Answers2

6

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[];

enter image description here

After evaluating the cell

 NotebookFind[SelectedNotebook[], "Print", All, CellStyle];

enter image description here

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[];

enter image description here

After the clicking the button:

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
1

With the help provided in the comments I have achieved almost exactly what I want using this code:

$Post = If[# =!= Null,
    CellPrint[
     TextCell["Are you sure you want to know the result?",
      CellTags -> "tag"]];
    CellPrint[ExpressionCell[
      Row@{
        Button["Yes",
         NotebookLocate["tag"]; NotebookDelete[]; 
         CellPrint[ExpressionCell[#, "Output"]]],
        Button["No", NotebookLocate["tag"]; NotebookDelete[]]}, 
      CellTags -> "tag"]]] &;

It uses CellPrint instead of Print and tags the created cells to locate and delete them afterwards. The only thing that does not quite work as I expected is that no matter where a cell is evaluated, after clicking the "Yes" button, the result is always printed at the very end of the notebook, and not where the actualy output should be.

einbandi
  • 4,024
  • 1
  • 23
  • 39