Mathematica 命令, 保存笔记本为 .wl, .wls, .m 等格式

FrontEndToken[]命令的帮助页面提到, 前段令牌菜单项键盘快捷键之间的映射在前端文本资源中定义.
在笔记本中输入$InstallationDirectory // SystemOpen 命令打开安装目录. 例如/usr/local/Wolfram/Mathematica/12.2/
再依次打开../SystemFiles/FrontEnd/TextResources, 这个目录就是前端文本资源.

其中CommonFrontEndInit.tr文件定义了笔记本页面菜单栏功能的函数实现. 例如与保存相关的功能定义如下:

AddFileBrowserFilterPacket["Save", {
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "NBLabel"], FEPrivate`FrontEndExecute[FrontEndToken[#2, "Save", {#, "Notebook"}]]&, "nb", "*.nb", MacintoshFileTypes->{"TEXT"}],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "CDFLabel"], FEPrivate`FrontEndExecute[FrontEndToken[#2, "Save", {#, "CDFNotebook"}]]&, "cdf", "*.cdf", MacintoshFileTypes->{"TEXT"}],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "WLLabel"], FEPrivate`FrontEndExecute[FrontEndToken[#2, "Save", {#, "Package"}]]&, "wl", "*.wl", MacintoshFileTypes->{"TEXT"}, OptionsNotebook -> "ExportPackage.nb"],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "MLabel"], FEPrivate`FrontEndExecute[FrontEndToken[#2, "Save", {#, "Package"}]]&, "m", "*.m", MacintoshFileTypes->{"TEXT"}, OptionsNotebook -> "ExportPackage.nb"],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "WLSLabel"], FEPrivate`FrontEndExecute[FrontEndToken[#2, "Save", {#, "Script"}]]&, "wls", "*.wls", MacintoshFileTypes->{"TEXT"}, OptionsNotebook -> "ExportPackage.nb"],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "PlainTextLabel"], FEPrivate`FrontEndExecute[FrontEndToken[#2, "Save", {#, "PlainText"}]]&, "txt", "*.txt", MacintoshFileTypes->{"TEXT"}],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "LatexLabel"],
          FrontEnd`SaveAs[#1, #2, "TeX"]&,
          "tex", "*.tex", MenuEvaluator->Automatic, MacintoshFileTypes->{"TEXT"}, OptionsNotebook -> "ExportTeX.nb"],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "PostScriptLabel"], FEPrivate`FrontEndExecute[NotebookPrint[#2, #1]]&, "ps", "*.ps", MacintoshFileTypes->{"TEXT"}, OptionsNotebook -> "ExportVectorFormat.nb"],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "PDFDocLabel"], FEPrivate`FrontEndExecute[NotebookPrint[#2, #1]]&, "pdf", "*.pdf", MacintoshFileTypes->{"PDF "}, OptionsNotebook -> "ExportPDF.nb"],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "RichTextFormatLabel"], FEPrivate`FrontEndExecute[FrontEndToken[#2, "Save", {#, "RTF"}]]&, "rtf", "*.rtf", MacintoshFileTypes->{"RTF "}],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "WebPageLabel"],
          FrontEnd`SaveAs[#1, #2, "XHTML"]&,
          "html", {"*.htm", "*.html"}, MenuEvaluator->Automatic, MacintoshFileTypes->{"TEXT"}, OptionsNotebook -> "ExportXHTML.nb"],
     Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "XMLLabel"],
          FrontEnd`SaveAs[#1, #2, "XHTMLMathML"]&,
          "xml", {"*.xml", "*.htm", "*.html"}, MenuEvaluator->Automatic, MacintoshFileTypes->{"TEXT"}, OptionsNotebook -> "ExportXHTMLMathML.nb" ]
}];

保存成包文件.wl格式的命令为

Item[FEPrivate`FrontEndResource["CommonFrontEndInitDialogs", "WLLabel"], 
FEPrivate`FrontEndExecute[FrontEndToken[#2, "Save", {#, "Package"}]]&, 
"wl", "*.wl", MacintoshFileTypes->{"TEXT"}, OptionsNotebook -> "ExportPackage.nb"]

itemFrontEndResource 是前端相关的函数, 真正执行保存的语句是FrontEndExecute. 如果想在笔记本中或脚本中使用, 可以使用如下方式:

FrontEndExecute[FrontEndToken[FrontEnd`EvaluationNotebook[], "Save", {
    StringTrim[NotebookFileName[], ".nb" ~~ EndOfString] <> ".wl", "Package"
    }]]

其中FrontEnd`EvaluationNotebook[] 是当前运行的笔记本.
StringTrim[NotebookFileName[], ".nb" ~~ EndOfString] <> ".wl"替换当前笔记本路径的拓展名为.wl.
"Package"指定保存格式为.wl格式.

根据programmatically saveas上老哥的说法, 使用下面两条命令效果也一样.

FrontEndExecute[ FrontEndToken[FrontEnd`EvaluationNotebook[],   "SaveRename", {"/../test.wl", "Package"}]]
FrontEndTokenExecute[FrontEnd`InputNotebook[],"SaveRename", {"/../test.wl", "Package"}
]

此外老哥还提到:

  • 这相当于手动执行文件>另存为...选择Mathematica软件包(*.m), 保存笔记本的初始化单元. 而其他单元被保存为(*注释*), 后者在脚本中调用的时候不会执行.
  • 如果您不提供文件路径, 文件将被保存在$HomeDirectory中, 这通常不是您想要的地方.
  • 如果文件不能被保存, 不会发出警告信息.

你可能感兴趣的:(Mathematica 命令, 保存笔记本为 .wl, .wls, .m 等格式)