Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Mathematica running under Windows 7. Is there a way to programmatically do a "save as " on an init.nb file to save it as init.m?

In the documentation center under tutorial/FrontEndTokens I found

FrontEndExecute[{FrontEndToken[FrontEnd`InputNotebook[],"SaveRenameSpecial", "Text"]}]

but it doesn't seem to work? I am looking for a working variation of this where "Text" is replaced by "Package" or somesuch.

Thanks for your help.

share|improve this question
To confirm, File > Save As... and Mathematica Package (*.m) manually does what you want, correct? – Mr.Wizard Jun 18 '12 at 16:54
@Mr.Wizard Yes, I should have made that more clear. Maual File>Save As... and Mathematica Package (*.m) works fine and is exactly what I would like to achieve programmatically. – Robert Miller Jun 18 '12 at 17:39
up vote 5 down vote accepted

The correct token syntax appears to be as follows:

   {"C:\\Data\\TestFile.m", "Package"}

This more compact form also works:

  {"C:\\Data\\TestFile.m", "Package"}

Please note that:

  • This is equivalent to doing File > Save As... select Mathematica Package (*.m) manually and will save only the Initialization cells of the Notebook. (The other cells are saved as (* comments *).)

  • If you do not provide a file path the file will end up in $HomeDirectory which is not usually where you want it.

  • No warning message is issued if the file cannot be saved.

share|improve this answer
Thank you! Exactly what I was looking for. – Robert Miller Jun 18 '12 at 18:12
@Robert I'm glad I could help. Finding these sometimes requires a bit of wizardry. ;-) – Mr.Wizard Jun 18 '12 at 18:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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