9

Inspired by this post I've been trying to implement the idea in Wolfram with no success yet. I've come pretty close though and need some help. So I've managed to do this:

NotebookSave[
 Notebook[{
   Cell[BoxData@MakeBoxes[
      f = NotebookFileName[]; NotebookClose[EvaluationNotebook[]]; 
      DeleteFile[f]
      ], "Input"]
   }
  , NotebookDynamicExpression :> 
   Refresh[FrontEndTokenExecute[EvaluationNotebook[], 
     "EvaluateNotebook"], UpdateInterval -> 0]
  , InitializationCellWarning -> False]
 , FileNameJoin[{$UserBaseDirectory, "SelfDestruct"}]
 ]

It saves the notebook to trusted directory with self destruct code in the input cell and it has dynamic expression that evaluates the notebook. Now the stupid question is how to prevent opening this notebook with NotebookSave or what would be the trick here? Because it immediately self destructs, but I want to preserve the file and do a self destruction later of course.

EDIT: Maybe there is a better way to do this? I also wonder whether it's possible to make self destructing CDF so it would work with just CDF Player installed.

swish
  • 7,881
  • 26
  • 48
  • Just a wild guess, but can't you, e.g., replace DeleteFile with something else, then edit the notebook in an external editor to restore DeleteFile? – LLlAMnYP Mar 09 '16 at 10:30
  • @LLlAMnYP Yeah it works, but kinda feels wrong :) – swish Mar 09 '16 at 10:34

1 Answers1

8

Use Export[file, your-nb-expression, "NB"] instead of NotebookSave.

Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420