创建 Palette 用于在已安装的 Mathematica 版本之间切换 .NB 关联
我想创建一个调色板,用于在 Windows 下安装的 Mathematica 版本之间切换 .NB 文件关联。
目前,我开发了以下代码,用于枚举已安装的 Mathematica 版本并在它们之间进行切换:
1) 获取已安装的 Mathematica 版本的系统注册表项名称列表:
installedVersions =
Select[Developer`EnumerateRegistrySubkeys["HKEY_CLASSES_ROOT"],
StringMatchQ[#, "Mathematica.NB." ~~ ___] || # ===
"MathematicaNB" &]
=> {"Mathematica.NB.7.0.1.1213965", "MathematicaNB"}
2) 添加 命令行选项 -b
(禁用启动屏幕)和 -directlaunch
(禁用启动安装的最新 Mathematica 版本的机制)到命令行字符串用于在系统注册表中启动Mathematica前端:
customizeOpenCommand[id_String] := Module[{value},
value =
Cases[Developer`ReadRegistryKeyValues[
"HKEY_CLASSES_ROOT\\" ~~ id ~~ "\\shell\\open\\command"],
Verbatim[Rule][Null,
val_String /;
StringFreeQ[val, " -b -directlaunch "]] :> (Null ->
StringReplace[val,
path__ ~~ "\\Mathematica.exe\"" ~~ __ ~~ "\"%1\"" :>
path ~~ "\\Mathematica.exe\" -b -directlaunch \"%1\""])];
Developer`WriteRegistryKeyValues[
"HKEY_CLASSES_ROOT\\" ~~ id ~~ "\\shell\\open\\command", value]];
该函数可按如下方式使用:
customizeOpenCommand /@ installedVersions
3) 用于获取当前.NB文件关联的函数:
Null /. Developer`ReadRegistryKeyValues["HKEY_CLASSES_ROOT\\.nb"]
4) 用于在所有Mathematica之间切换的按钮> 安装的版本(但我认为使用Dynamic
和SetterBar
可能可以更好地实现):
Column[Button[
Row[{"Associate .NB-files with ", Style[#, Bold], " (",
First@Cases[
Developer`ReadRegistryKeyValues["HKEY_CLASSES_ROOT\\" ~~ #],
Verbatim[Rule][Null, str_String] :> str], ")"}],
Developer`WriteRegistryKeyValues["HKEY_CLASSES_ROOT\\.nb",
Null -> #], Alignment -> Left] & /@ installedVersions]
除了上面的之外,这里还有一个禁止在不同版本之间共享首选项的命令安装的 Mathematica 版本(默认情况下,所有安装的版本都使用一个文件来存储前端设置):
SetOptions[$FrontEnd, "VersionedPreferences" -> True]
所以我的问题是:
如何创建和安装一个小的 Palette
它将动态显示.NB 文件的当前文件关联并允许通过单击按钮在它们之间切换?我认为它可能可以仅使用 SetterBar
来实现,但我对 Dynamic
和调色板创建仍然没有经验。
I would like to create a palette for switching .NB-file associations between installed versions of Mathematica under Windows.
At this moment I have developed the following code for enumerating installed versions of Mathematica and switching between them:
1) Getting list of names of system registry keys of installed versions of Mathematica:
installedVersions =
Select[Developer`EnumerateRegistrySubkeys["HKEY_CLASSES_ROOT"],
StringMatchQ[#, "Mathematica.NB." ~~ ___] || # ===
"MathematicaNB" &]
=> {"Mathematica.NB.7.0.1.1213965", "MathematicaNB"}
2) Function that adds command line options -b
(disables the splash screen) and -directlaunch
(disables the mechanism which launches the most recent Mathematica version installed) to the command line string for launching Mathematica FrontEnd in the system registry:
customizeOpenCommand[id_String] := Module[{value},
value =
Cases[Developer`ReadRegistryKeyValues[
"HKEY_CLASSES_ROOT\\" ~~ id ~~ "\\shell\\open\\command"],
Verbatim[Rule][Null,
val_String /;
StringFreeQ[val, " -b -directlaunch "]] :> (Null ->
StringReplace[val,
path__ ~~ "\\Mathematica.exe\"" ~~ __ ~~ "\"%1\"" :>
path ~~ "\\Mathematica.exe\" -b -directlaunch \"%1\""])];
Developer`WriteRegistryKeyValues[
"HKEY_CLASSES_ROOT\\" ~~ id ~~ "\\shell\\open\\command", value]];
This function can be used as follows:
customizeOpenCommand /@ installedVersions
3) Function for getting current .NB-file association:
Null /. Developer`ReadRegistryKeyValues["HKEY_CLASSES_ROOT\\.nb"]
4) Buttons for switching between all Mathematica versions installed (but I think it probably could be implemented better using Dynamic
and SetterBar
):
Column[Button[
Row[{"Associate .NB-files with ", Style[#, Bold], " (",
First@Cases[
Developer`ReadRegistryKeyValues["HKEY_CLASSES_ROOT\\" ~~ #],
Verbatim[Rule][Null, str_String] :> str], ")"}],
Developer`WriteRegistryKeyValues["HKEY_CLASSES_ROOT\\.nb",
Null -> #], Alignment -> Left] & /@ installedVersions]
In addition to the above here is the command which disables sharing of preferences between different Mathematica versions installed (by default all installed versions use one file for storing the FrontEnd settings):
SetOptions[$FrontEnd, "VersionedPreferences" -> True]
So my problem is:
How to create and install a small Palette
which will dynamically display the current file associations for .NB-files and will allow to switch between them by clicking a button? I think it probably can be implemented just with SetterBar
but I still unexperienced with Dynamic
and palette creation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止,我已经得出以下解决方案:
可以使用“调色板”菜单中的“安装调色板...”菜单项永久安装该调色板。
其外观如下:
欢迎任何建议和改进!
由于Mathematica的所有已安装版本共享相同的
$BaseDirectory
和$UserBaseDirectory
,因此建议在安装的最旧版本中安装此调色板,以避免标准警告弹出窗口:Up to this moment I have come to the following solution:
This palette can be installed permanently using "Install Palette..." menu item in "Palette" menu.
Here is how it looks:
Any suggestions and improvements are welcome!
Since all installed versions of Mathematica share the same
$BaseDirectory
and$UserBaseDirectory
, it is recommended to install this palette in the oldest version installed to avoid standard warning popup: