【问题标题】:Creating Palette for switching .NB-associations between installed versions of Mathematica创建用于在已安装的 Mathematica 版本之间切换 .NB 关联的调色板
【发布时间】:2011-09-16 23:41:34
【问题描述】:

我想创建一个调色板,用于在 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) 将command line options-b(禁用启动屏幕)和-directlaunch(禁用启动最新安装的Mathematica版本的机制)添加到命令行字符串的函数在系统注册表中启动 Mathematica FrontEnd:

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 和调色板创建经验。

【问题讨论】:

    标签: windows wolfram-mathematica registry file-association mathematica-frontend


    【解决方案1】:

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

    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];
    

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

    这是它的外观:

    欢迎提出任何建议和改进!


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

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

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 2016-05-26
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2021-01-29
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多