创建 Palette 用于在已安装的 Mathematica 版本之间切换 .NB 关联

发布于 2024-11-15 18:39:02 字数 2330 浏览 4 评论 0原文

我想创建一个调色板,用于在 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之间切换的按钮> 安装的版本(但我认为使用DynamicSetterBar可能可以更好地实现):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-11-22 18:39:02

到目前为止,我已经得出以下解决方案:

CreatePalette@
  Framed[DynamicModule[{b, installedVersions}, 
    Dynamic[Column[
      Join[{Style["Associate .NB-files with:", Bold], 
        SetterBar[
         Dynamic[val, 
          Function[{v, e}, 
           Developer`WriteRegistryKeyValues["HKEY_CLASSES_ROOT\\.nb", 
            Null -> v]; 
           e = Null /. 
             Developer`ReadRegistryKeyValues[
              "HKEY_CLASSES_ROOT\\.nb"], HoldRest], 
          Initialization -> (installedVersions = 
             Select[Developer`EnumerateRegistrySubkeys[
               "HKEY_CLASSES_ROOT"], 
              StringMatchQ[#, "Mathematica.NB." ~~ ___] || # === 
                 "MathematicaNB" &]; 
            If[StringFreeQ[
                value = 
                 Null /. 
                  Developer`ReadRegistryKeyValues[
                   "HKEY_CLASSES_ROOT\\" ~~ # ~~ 
                    "\\shell\\open\\command"], " -b -directlaunch "], 
               Developer`WriteRegistryKeyValues[
                "HKEY_CLASSES_ROOT\\" ~~ # ~~ 
                 "\\shell\\open\\command", 
                Null -> 
                 StringReplace[value, 
                  path__ ~~ "\\Mathematica.exe\"" ~~ __ ~~ "\"%1\"" :>
                    path ~~ 
                    "\\Mathematica.exe\" -b -directlaunch \"%1\""]]] \
& /@ installedVersions)], installedVersions, 
         Appearance -> "Vertical"]}, 
       If[Last@Last@Options[$FrontEnd, "VersionedPreferences"] === 
          False && 
         b == True, {Button[
          Pane[Style[
            "This FrontEnd uses shared preferences file. Press this \
button to set FrontEnd to use versioned preferences file (all the \
FrontEnd settings will be reset to defaults).", Red], 300], 
          AbortProtect[
           SetOptions[$FrontEnd, "VersionedPreferences" -> True]; 
           b = False]]}, {}]], Alignment -> Center], 
     Initialization :> 
      If[! Last@Last@Options[$FrontEnd, "VersionedPreferences"], 
       b = True, b = False]]], FrameMargins -> {{0, 0}, {0, 5}}, 
   FrameStyle -> None];

可以使用“调色板”菜单中的“安装调色板...”菜单项永久安装该调色板。

其外观如下:

screenshot1 screenshot2

欢迎任何建议和改进!


由于Mathematica的所有已安装版本共享相同的$BaseDirectory$UserBaseDirectory,因此建议在安装的最旧版本中安装此调色板,以避免标准警告弹出窗口:

此笔记本是在更新版本的 Mathematica 中创建的,并且
可能无法与您使用的旧前端一起正常工作。
请联系 Wolfram Research (www.wolfram.com) 了解升级信息。

Up to this moment I have come to the following solution:

CreatePalette@
  Framed[DynamicModule[{b, installedVersions}, 
    Dynamic[Column[
      Join[{Style["Associate .NB-files with:", Bold], 
        SetterBar[
         Dynamic[val, 
          Function[{v, e}, 
           Developer`WriteRegistryKeyValues["HKEY_CLASSES_ROOT\\.nb", 
            Null -> v]; 
           e = Null /. 
             Developer`ReadRegistryKeyValues[
              "HKEY_CLASSES_ROOT\\.nb"], HoldRest], 
          Initialization -> (installedVersions = 
             Select[Developer`EnumerateRegistrySubkeys[
               "HKEY_CLASSES_ROOT"], 
              StringMatchQ[#, "Mathematica.NB." ~~ ___] || # === 
                 "MathematicaNB" &]; 
            If[StringFreeQ[
                value = 
                 Null /. 
                  Developer`ReadRegistryKeyValues[
                   "HKEY_CLASSES_ROOT\\" ~~ # ~~ 
                    "\\shell\\open\\command"], " -b -directlaunch "], 
               Developer`WriteRegistryKeyValues[
                "HKEY_CLASSES_ROOT\\" ~~ # ~~ 
                 "\\shell\\open\\command", 
                Null -> 
                 StringReplace[value, 
                  path__ ~~ "\\Mathematica.exe\"" ~~ __ ~~ "\"%1\"" :>
                    path ~~ 
                    "\\Mathematica.exe\" -b -directlaunch \"%1\""]]] \
& /@ installedVersions)], installedVersions, 
         Appearance -> "Vertical"]}, 
       If[Last@Last@Options[$FrontEnd, "VersionedPreferences"] === 
          False && 
         b == True, {Button[
          Pane[Style[
            "This FrontEnd uses shared preferences file. Press this \
button to set FrontEnd to use versioned preferences file (all the \
FrontEnd settings will be reset to defaults).", Red], 300], 
          AbortProtect[
           SetOptions[$FrontEnd, "VersionedPreferences" -> True]; 
           b = False]]}, {}]], Alignment -> Center], 
     Initialization :> 
      If[! Last@Last@Options[$FrontEnd, "VersionedPreferences"], 
       b = True, b = False]]], FrameMargins -> {{0, 0}, {0, 5}}, 
   FrameStyle -> None];

This palette can be installed permanently using "Install Palette..." menu item in "Palette" menu.

Here is how it looks:

screenshot1 screenshot2

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:

This notebook was created in a more recent version of Mathematica, and
may not function properly with the older front end you are using.
Contact Wolfram Research (www.wolfram.com) for upgrade information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文