2

If you do Ctrl+Shift+o you open an option inspector.

And then you can enable AutoPackageGenerated which allows you to automatically generate .m from .nb input.

Unfortunately as discussed in this question the outputted .m is not formatted.

How might I patch such?

William
  • 7,595
  • 2
  • 22
  • 70

1 Answers1

1

This only exports output cells, but does work.

SetOptions[EvaluationNotebook[], NotebookEventActions -> {
   {"MenuCommand", "Save"} :> 
    Module[{loc, nb, new = FileNameJoin@Append[
         Drop[FileNameSplit@NotebookFileName[], -1],
         FileBaseName@FileNameTake[NotebookFileName[]] <> ".m"
         ]
      },
     NotebookSave[InputNotebook[]];
     loc = FileNameJoin[{$TemporaryDirectory, "output.txt"}]; 
     nb = 
      CreateDocument[NotebookGet[InputNotebook[]][[1]], 
       Visible -> False];
     FrontEndExecute[FrontEndToken[nb, "Save", {loc, "Text"}]];
     NotebookClose[nb];
     DeleteFile[new];
     CopyFile[loc, new];
     ]
   }]
William
  • 7,595
  • 2
  • 22
  • 70