19
$\begingroup$

Is it possible to change the way Mathematica saves so that Out[] lines are never included?

I have a .nb file that processes a lot of data, and displays most of it as standard output. These graphs, tables, and intensity plots almost double the size of the file. I'd like to easily save these files in a smaller form and also prevent accidentally sending confidential results to users outside my company or not under an NDA.

That is, of course, without using Cell -> Delete All Output before each save. I can't be expected to remember to do that all the time! :-P

$\endgroup$

2 Answers 2

12
$\begingroup$

In Options Inspector you can change the EvaluationOptions for Selected Notebook or for Global Preferences by adding

  {"WindowClose" :>  FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]]}

to the NotebookEventActions line.

Screenshots for changing the notebook evaluation options in Options Inspector:

A notebook with Input and Output cells and the Evaluation Options page in Option Inspector

before

After editing the NotebookEventActions line in Options Inspector or evaluating

SetOptions[EvaluationNotebook[], NotebookEventActions -> {"WindowClose" :> 
 FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]]}]; 

inside the notebook:

after edit

After clicking X on the on the notebooks window frame:

after window close

After clicking Yes on the dialog window:

after yes

Alternative: you can add

 SetOptions[EvaluationNotebook[], NotebookEventActions -> {"WindowClose" :> 
 FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]]}]; 

to a notebook's initialization cell.

$\endgroup$
1
  • 1
    $\begingroup$ +1 This is probably a better solution than mine. It can be added at a per-notebook level while mine has to be global. Also, the final SetOptions approach is very quick to do. $\endgroup$
    – Simon
    Feb 23, 2012 at 21:44
11
$\begingroup$

After making a backup of the MenuSetup.tr file, you can insert the following line somewhere in the Menu["&File"...] section:

MenuItem["Delete output and Save", FrontEndExecute[{
    FrontEndToken["DeleteGeneratedCells"], FrontEndToken["Save"]}], 
  MenuKey["X", Modifiers->{"Control", "Shift"}]],

I placed it beneath the MenuItem["&Save"...] line. You can also change the key combination to something more memorable.


To see a list of all frontend tokens go to https://stackoverflow.com/a/4209612/421225

And the following code should give the location of MenuSetup.tr on all three supported platforms

With[{os = Switch[$OperatingSystem,
    "Unix", "X",
    "MacOSX", "OSX",
    "Windows", "Windows"]}, 
 FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", 
   "TextResources", os, "MenuSetup.tr"}]]
$\endgroup$
1
  • $\begingroup$ @Mr.Wizard: Yeah been busy, but today I had time to procrastinate. Nice moderator diamond, btw! $\endgroup$
    – Simon
    Feb 23, 2012 at 13:15

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.