9
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – LLlAMnYP Mar 9 '16 at 10:30
  • $\begingroup$ @LLlAMnYP Yeah it works, but kinda feels wrong :) $\endgroup$ – swish Mar 9 '16 at 10:34
8
$\begingroup$

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

$\endgroup$
  • 3
    $\begingroup$ Yeah! It works! I tried Export before but without NB, I thought it doesn't work, thanks! $\endgroup$ – swish Mar 9 '16 at 10:42
  • $\begingroup$ @swish Glad it worked for you. Yours is pretty inventive code, by the way. Nice! $\endgroup$ – Leonid Shifrin Mar 9 '16 at 10:44
  • $\begingroup$ I thought it will fix my problems but unfortunately it won't, could you take a look at 109651? $\endgroup$ – Kuba Mar 10 '16 at 14:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.