Is there a straightforward way to reference and modify a cell from within itself?
This is a little outside the ordinary use case and certainly not good practice, but I would like to execute an input cell in a notebook causing the kernel to perform some action and output results and then replace or delete the current input cell and it's output.
I currently use some trivial cleanup code at the end of a routine to process data and after it has run once it isn't useful to have around and I end up deleting it manually. This got me thinking, how can I get Mathematica to do this automatically? Maybe it's possible to build it into the notebook style? Something like:
Format --> Style --> RunOnceAndDeleteInput
NotebookDelete
seems to be useful for clobbering cells and output, but it's not clear to me how to reference an input cell from within itself. $\endgroup$EvaluationCell
to reference an input cell from within itself . So a cell with the linesSelectionMove[EvaluationNotebook[], All, EvaluationCell];NotebookDelete[]
finds and deletes itself. $\endgroup$SelectionMove[EvaluationNotebook[],All,EvaluationCell];NotebookDelete[];
works great for clobbering a cell. $\endgroup$NotebookWrite
can be used with the cell selector code and you've got everything you need for arbitrary self-modifying cells. $\endgroup$