简体   繁体   中英

How to find the default runner application for a windows file extension in Javascript/Extendscript

I am creating an extendscript that requires the verification that python is installed on the machine. In order to do this, I want a function that looks somewhat like this:

function defaultApp(fileExtension) { return defaultAppName; }

And then check if the default app name is 'python.exe'. From my understanding (that had gathered from another similar post that implements a solution using the python winreg library), the windows registry should be accessed to get such information.

You can run a bat file something like this:

python --version > d:\p.txt

And then to check the content of the txt file. If Python is installed (and configured) you will get info about a version of the Python. If there is no Python you will get empty txt file.

It can be something like this:

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();

This solution is inspired by Yuri Khristich's answer. (A more compact version)

//@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 and File.remove() are not built-in functions. I created them to help declutter my code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM