如何解决如何在 Javascript/Extendscript 中找到 Windows 文件扩展名的默认运行器应用程序
我正在创建一个需要验证机器上安装了 python 的扩展脚本。为了做到这一点,我想要一个看起来像这样的函数:
function defaultApp(fileExtension) { return defaultAppName; }
然后检查默认应用程序名称是否为“python.exe”。 根据我的理解(从另一篇使用 python winreg 库实现解决方案的类似帖子中收集到的),应该访问 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() 不是内置函数。我创建它们是为了帮助整理我的代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。