Skip to main content
1 of 2
dionys
  • 4.3k
  • 1
  • 20
  • 46

How to write Mma notebook input that modifies itself?

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 delete the cell automatically? Maybe it's possible to build it into the notebook style? Something like:

Format --> Style --> RunOnceAndDeleteInput

dionys
  • 4.3k
  • 1
  • 20
  • 46