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

1 Answer 1

8
$\begingroup$

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

$\endgroup$
3
  • 3
    $\begingroup$ Yeah! It works! I tried Export before but without NB, I thought it doesn't work, thanks! $\endgroup$
    – swish
    Commented Mar 9, 2016 at 10:42
  • $\begingroup$ @swish Glad it worked for you. Yours is pretty inventive code, by the way. Nice! $\endgroup$ Commented Mar 9, 2016 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
    Commented Mar 10, 2016 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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