Search code examples
javascriptadobe-indesign

fix doScript function error in InDesign script


I wrote a script for InDesign and used doScript to run a bat file. This script works on some systems and not on others. The error is in the image.

enter image description here

I run as admin InDesign. But it gives another error.

enter image description here

How can I fix the error?

function batFile(str) {
    var path = "~\\AppData\\Roaming\\test\\";
    var filename = 'b1.bat';

    var file1 = new File(path + filename);
        file1.encoding = 'UTF-8';
        file1.open('w');
        var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">%appdata%\\test\\t1.txt"
        file1.write(txt);
        file1.close();

    var cmdcode = "CreateObject(\"WScript.Shell\").Run \"%appdata%\\test\\b1.bat\", 0, True";
    app.doScript(cmdcode, ScriptLanguage.visualBasic, undefined, UndoModes.FAST_ENTIRE_SCRIPT);
    var result = path + "t1.txt";
    var arry = openFile2(result);
    
    if (arry.length != 0) {
        return arry;
    } else {
        return "null";
    }
}

**

Update

I find the problem. When a user name is composed of two parts, such as "your name" causes this problem. To address this issue, we need to put the address in two double quotations.

var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">\"%appdata%\\test\\t1.txt"\"
    

** Update 2

In Windows, when the user uses OneDrive, the AppData path also changes, which causes doScript not to run. for example:

c: \ users \ username \ appdata

Changes to:

c: \ users \ username \ onedrive \ appdata


Solution

  • I can't tell what the problem with your script. But just in case, you can run bat file this way:

    var bat_file = File("your_file.bat");
    bat_file.execute();
    

    Update

    Based on your code I can offer the workaround: add & echo ok > %appdata%\\test\\ok.txt in your bat file and check if the file ok.txt exists before going further.

    function batFile(str) {
        var path = "~\\AppData\\Roaming\\test\\";
        var filename = 'b1.bat';
    
        var file1 = new File(path + filename);
            file1.encoding = 'UTF-8';
            file1.open('w');
            var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">%appdata%\\test\\t1.txt";
            // add the file 'ok.txt' after the previous command is finished
            txt += " & echo ok > %appdata%\\test\\ok.txt";
            file1.write(txt);
            file1.close();
    
        // run the bat file
        file1.execute();
    
        // check if the file 'ok.txt' exists before going further
        var ok = File(path + "ok.txt");
        while (!ok.exists) $.sleep(100);
        ok.remove();
    
        // do stuff
        var result = path + "t1.txt";
        var arry = openFile2(result);
    
        if (arry.length != 0) {
            return arry;
        } else {
            return "null";
        }
    }