无法使用 Java 在 AutoIt 中打开记事本

Not able to open notepad in AutoIt with Java

这里我尝试用 Java 在 AutoIt 中打开记事本。但是我无法使用以下代码打开它。

public class Notepad {
    public static String jvmBitVersion() { //returning the JVM Version
        return System.getProperty("sun.arch.data.model");
    }

    public static void main(String[] args) throws InterruptedException {
        String jacobDllVersionToUse;
        System.out.println("JVM version is: " +jvmBitVersion());

        if (jvmBitVersion().contains("32")) {   //Checking for the JVM Version
            jacobDllVersionToUse = "jacob-1.18-M2-x86.dll"; // If the version is 32- bit use this.
        }
        else { // enter code here
            jacobDllVersionToUse = "jacob-1.18-M2-x64.dll"; // if the version is 64-bit go for this
        }

        File file = new File("lib", jacobDllVersionToUse); // file location for jacob
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());

        AutoItX x = new AutoItX();
        x.run("notepad.exe"); // trying to open the notepad

        x.winActivate("Untitled - Notepad"); // waiting for the notepad to open
        x.winWaitActive("Untitled - Notepad");

        x.send("This is some text"); // Once the notepad is open write into it.

    }
}

如果代替 notepad.exe 我给 calc.exe 它工作正常。如果我在 运行 之后手动打开记事本,这是写入记事本的代码。

尝试完整路径:

x.run("C:\Windows\System32\notepad.exe")

而且我会为您的等待命令使用不同的顺序,因为您无法激活 "doesn't" 存在的 window 并且在之前激活 window 会更聪明你使用 send() 命令。正确的顺序是:

x.winWaitActive("Untitled - Notepad");

x.winActivate("Untitled - Notepad");
x.send("This is some text");

您也可以只使用 WinWait()

这段代码对我有用:

AutoItX x = new AutoItX();
x.run("notepad.exe", "C:/Windows/System32/", AutoItX.SW_SHOW); // trying to open the notepad

x.winActivate("Sin título: Bloc de notas"); // waiting for the notepad to open
x.winWaitActive("Sin título: Bloc de notas");
x.send("This is some text"); // Once the notepad is open write into it.

请注意 window 的标题必须与 Windows 应用程序匹配。就我而言,Windows 是西班牙语,标题是 "Sin título: Bloc de notas".

立即启动记事本 shown 将解决此问题。

记事本

AutoItX x = new AutoItX();
x.run("notepad.exe", "", AutoItX.SW_SHOW);
x.winActivate("Untitled - Notepad");
x.winWaitActive("Untitled - Notepad");
x.send("This is some text");

Notepad++

AutoItX x = new AutoItX();
x.run("C:\Program Files (x86)\Notepad++\notepad++.exe");
x.winActivate("[CLASS:Notepad++]");
x.winWaitActive("[CLASS:Notepad++]");
x.send("This is some text");