如何在Javascript/Extendscript中找到windows文件扩展名的默认运行程序应用程序



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

function defaultApp(fileExtension) { return defaultAppName; }

然后检查默认应用程序名称是否为"python.exe"。根据我的理解(这是从另一篇使用pythonwinreg库实现解决方案的类似文章中收集到的(,应该访问windows注册表来获取这些信息。

您可以运行类似以下的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();

此解决方案的灵感来自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((不是内置函数。我创建它们是为了帮助清理我的代码。

最新更新