【问题标题】:How to find the default runner application for a windows file extension in Javascript/Extendscript如何在 Javascript/Extendscript 中找到 Windows 文件扩展名的默认运行程序应用程序
【发布时间】:2021-07-09 14:11:22
【问题描述】:

我正在创建一个扩展脚本,它需要验证机器上是否安装了 python。为了做到这一点,我想要一个看起来像这样的函数:

function defaultApp(fileExtension) { return defaultAppName; }

然后检查默认应用名称是否为“python.exe”。 根据我的理解(从另一个使用 python winreg 库实现解决方案的类似帖子中收集),应该访问 Windows 注册表以获取此类信息。

【问题讨论】:

    标签: visual-studio-code adobe extendscript winreg


    【解决方案1】:

    你可以像这样运行一个bat文件:

    python --version > d:\p.txt
    

    然后去查看txt文件的内容。如果 Python 已安装(并配置),您将获得有关 Python 版本的信息。如果没有 Python,你会得到一个空的 txt 文件。

    可能是这样的:

    function check_python() {
    
        // create the bat file
        var bat_file = File(Folder.temp + "/python_check.bat");
        bat_file.open("w");
        bat_file.write("python --version > %temp%/python_check.txt");
        bat_file.close();
    
        // check if the bat file was created
        if (!bat_file.exists) {
            alert ("Can't check if Python is installed");
            return false;
        }
    
        // run the bat file
        bat_file.execute();
        $.sleep(300); 
    
        // check if the txt file exists
        var check_file = File(Folder.temp + "/python_check.txt");
        if (!check_file.exists) { 
            alert ("Can't check if Python is installed"); 
            bat_file.remove();
            return false;
        }
    
        // get contents of the txt file
        check_file.open("r");
        var contents = check_file.read();
        check_file.close();
    
        // check the contents
        if (contents.indexOf("Python 3") != 0) { 
            alert("Python 3 is not found"); 
            bat_file.remove();
            check_file.remove();
            return false;
        }
    
        // hooray!
        alert("Python is found!")
        bat_file.remove();
        check_file.remove();
        return true;
    }
    
    var is_python = check_python();
    

    【讨论】:

    • 这实际上是一个简单但有趣的解决方案,适用于所有平台。我现在要使用它,而不是通过检查 win 注册表或其他方式来做“丑陋的方式”。
    • 如果您好奇,请查看我自己的答案,其中我尝试使用一个小实用程序来实现您的脚本的更紧凑版本。
    • 是的。你的版本看起来很聪明。我会用它。尽管如此,这两个版本都只能在 Windows 上运行。 MacOS 将需要另一个实现。
    • 我以前从未使用过 Mac,所以我不知道。但我认为同样的逻辑也适用。
    • 现在我很好奇,MacOS 实现还需要哪些其他更改?
    【解决方案2】:

    此解决方案的灵感来自 Yuri Khristich 的回答。 (更紧凑的版本)

    //@include "utils/$file.jsx";
    function ispy()
    {
        var ispy,
            cmd = "python --version > %temp%/pycheck.txt",
            chk = File(Folder.temp + "/pycheck.txt").$create(),
            bat = File(Folder.temp + "/pycheck.bat").$create(cmd);
        
        bat.$execute(100);
        ispy = !!chk.$read();
        //cleanup:
        File.remove(bat, chk);
        return ispy;
    }
    
    $.writeln(ispy()) //true
    

    $read、$create、$execute 和 File.remove() 不是内置函数。我创建它们是为了帮助整理我的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-23
      • 2016-07-18
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      相关资源
      最近更新 更多