7
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ To confirm, File > Save As... and Mathematica Package (*.m) manually does what you want, correct? $\endgroup$ Commented Jun 18, 2012 at 16:54
  • $\begingroup$ @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. $\endgroup$ Commented Jun 18, 2012 at 17:39

1 Answer 1

8
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Thank you! Exactly what I was looking for. $\endgroup$ Commented Jun 18, 2012 at 18:12
  • $\begingroup$ @Robert I'm glad I could help. Finding these sometimes requires a bit of wizardry. ;-) $\endgroup$ Commented Jun 18, 2012 at 18:15
  • 1
    $\begingroup$ It seems like this may no longer be functional. At least for Mathematica 11.0.1 on a Mac the command does nothing. Both "SaveRename" and "SaveRenameSpecial" bare will open the dialogs, but passing a file name seems to do nothing. $\endgroup$ Commented Dec 9, 2016 at 6:54
  • $\begingroup$ @MB1965 Thanks for the note. I am still using 10.1.0 and run Windows so I cannot attempt to find a solution. If this is important to you please post a new question with a link back to this one, and note the version specific problem. $\endgroup$ Commented Dec 9, 2016 at 7:10
  • 3
    $\begingroup$ @Mr.Wizard it turns out it merely doesn't like the ~ shell escape that's the standard on Unix. If I give it an absolute path the package generates fine. No idea why I didn't check that earlier. $\endgroup$ Commented Dec 9, 2016 at 9:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.