Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am using Mathematica 8.0.4.0 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
1  
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

1 Answer 1

up vote 4 down vote accepted

The correct token syntax appears to be as follows:

FrontEndExecute[
 FrontEndToken[
   FrontEnd`InputNotebook[], 
   "SaveRename",
   {"C:\\Data\\TestFile.m", "Package"}
 ]
]

This more compact form also works:

FrontEndTokenExecute[
  FrontEnd`InputNotebook[],
  "SaveRename",
  {"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

 
discard

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.