2

Is there a way to create Notebook in which each Initialization Cell will be auto-saved in its own .m-file with arbitrary name?

P.S. The question is related to the Mathematica program developed by Wolfram Research Inc. It is not about Mathematics or math.

Alexey Popkov
  • 9,355
  • 4
  • 42
  • 93
  • I don't know the answer, but just curious: Why do you want to do that? – Dr. belisarius Apr 08 '11 at 03:29
  • I don't think it can be done. You could probably write an initialization cell that evaluated the code and wrote it out to a unique file, but I think that would just happen when the initialization cell evaluated, and not every time the file got saved. – Brett Champion Apr 08 '11 at 03:39
  • @belisarius The reason is that I am developing an application intended to use slave kernels through MathLink. I need a separate initialization file for that kernels. And I feel it very comfortable to have the all my code in **one** Notebook. – Alexey Popkov Apr 08 '11 at 03:41
  • And I think there are better/more reliable ways to generate packages than through the initialization-cell-saving mechanism from the old days. (Workbench, text editors, and New > Pacakge, combined with Get statements all work well, depending on the task at hand.) – Brett Champion Apr 08 '11 at 03:42
  • @Brett In what meaning initialization-cell-saving mechanism is not reliable? – Alexey Popkov Apr 08 '11 at 03:55
  • 3
    I think the auto-saving mechanism is reliable, as long as you only ever edit the notebook. Invariably though it seems that I then wanted to edit the corresponding .m file, at which point there's a divergence in the code. You could probably use Put or Export to generate the .m file for the slave kernels, and create a button to save the file. It'd be a bit more manual that desired, but the button could be put in a Section cell grouping the code, or in a custom docked cell for the authoring notebook... – Brett Champion Apr 08 '11 at 04:04
  • Alexey, would you consider Brett's suggestion in the comment above to be a solution? Do you need help implementing it? – Mr.Wizard Apr 11 '11 at 04:58
  • The Brett's method is interesting and I would appreciate a working implementation. Knowing that in really the initialization file is used only when I launch a slave kernel it would be nice to have a function that autosaves (if it is changed from the last autosaving) the current version of the cell in the .m-file before launching the kernel. The button at the same time would be also useful. – Alexey Popkov Apr 11 '11 at 05:37
  • But at this moment I see another, possibly even better alternative although I am not sure that it is a reliable way: to pass on the fly the current content of the cell with initialization expressions for the slave kernels to each of them when launching it. I am doubt a little about `BoxForm` version compatibility: I work with slave kernels of *Mathematica* 5.2. But we could convert cell into `InputForm` on the fly too. – Alexey Popkov Apr 11 '11 at 05:37
  • Alexey, please make sure to use the @name feature when replying to someone other than the topic starter (you, in this case). I only saw your replies now. – Mr.Wizard Apr 15 '11 at 20:05

1 Answers1

1

I'm not sure if the following approach would satisfy you: I once wanted a way of generating compact notebooks containing only the initialisation cells found in my development notebook; the following code writes the initialization cells of the current notebook into a single new notebook and autosaves a .m file as a side-effect but it could easily be adapted to generate a separate notebook and .m file for each initialization cell.

In[162]:= nbToExtract = SelectedNotebook[]

In[163]:= 
extractInitializationCells[nb_] :=
 Block[{nbNew = CreateDocument[], count = 0},
  (SelectionMove[nb, Next, Cell];
   While[NotebookRead[nb] =!= {}, (If[InitializationCell /. 
          Options[NotebookSelection[nb], InitializationCell],
      (count++;
       NotebookWrite[nbNew, NotebookRead[nb]]), {}]; SelectionMove[nb, Next, Cell])];
   Print[ToString[count] <> " initialization cell(s) found"];
   CurrentValue[nbNew, AutoGeneratedPackage] = Automatic;
   NotebookSave[nbNew, fn];
   NotebookClose[nbNew];
   Clear[nbNew](* just in case *))]

extractInitializationCells[nbToExtract]

This only extracts the initialisation cells below the cell in which the function extractInitializationCells is called. And I'd agree with the previous caveats about using the auto generation package mechanism. Also, CurrentValue is not protected indefinitely from backwards incompatibility but it has survived several major Mathematica versions so far.

fairflow
  • 445
  • 3
  • 12
  • Oh, and @BrettChampion's idea of a button or similar would be needed for this approach; it wouldn't be automatic on save. There's probably a way to modify the front end to run this on every save though. I wouldn't know how to do that. – fairflow Feb 14 '13 at 17:17