2
votes

is there a way to run a script with parameters from java? Not an executable file, but an AutoHotKey script.

I tried this, but since it is not a valid executable file it doesn't work.

Control class :

package org.bsep.acp;

import java.io.IOException;

/**
 * This class allow you to send string to your
 * computer as keystrokes.
 * 
 * escape car is '
 * special char are {space}, {Enter}, {F1}, {F2}, etc
 * 
 * @author Eildosa
 */
public class StringSender {

    Runtime runtime;
    private final static String AHK_BRIDGE = "C:\\perso\\WorkspaceScripts\\skyrimTools\\src\\org\\bsep\\acp\\ahkBridge.ahk";

    public StringSender() {
        runtime = Runtime.getRuntime();
    }

    public void sendString(String data) throws IOException, InterruptedException {
        runtime.exec(new String[] { AHK_BRIDGE, data} );
        Thread.currentThread();
        Thread.sleep(1000);
    }

}

Test :

Runtime runtime = Runtime.getRuntime();
runtime.exec(NOTEPAD);
Thread.currentThread();
Thread.sleep(4000);
StringSender stringSender = new StringSender();
stringSender.sendString("Writing from java through AHK.");

Exception :

Exception in thread "main" java.io.IOException: Cannot run program "C:\perso\WorkspaceScripts\skyrimTools\src\org\bsep\acp\ahkBridge.ahk": CreateProcess error=193, %1 n?est pas une application Win32 valid
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
    at java.lang.Runtime.exec(Runtime.java:617)
    at java.lang.Runtime.exec(Runtime.java:485)
    at org.bsep.acp.StringSender.sendString(StringSender.java:25)
    at org.bsep.acp.VariousTests.ahkBridgeTester(VariousTests.java:23)
    at org.bsep.acp.VariousTests.main(VariousTests.java:13)
Caused by: java.io.IOException: CreateProcess error=193, %1 n?est pas une application Win32 valid
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:376)
    at java.lang.ProcessImpl.start(ProcessImpl.java:136)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
    ... 5 more

Translation : this is not a valid win32 application.

Thanks.

2
What do AHK_BRIDGE and data contain? Please post a sufficient amount of code.MCL

2 Answers

6
votes

Since raw AHK scripts aren't executable, you can either compile your script and execute it directly or pass the path of your script as an argument to AutoHotkey.exe (usually located in C:\Program Files\AutoHotkey\). Regarding the second option, your code could look like this:

public void sendString(String data) throws IOException, InterruptedException {
    String ahkPath = "C:\\Program Files\\AutoHotkey\\AutoHotkey.exe";
    String scriptPath = "C:\\Users\\MCL\\test.ahk";
    runtime.exec(new String[] { ahkPath, scriptPath, data} );
    Thread.currentThread();
    Thread.sleep(1000);
}

AutoHotkey will pass on each argument to the script, starting with the second (in this case: data).

0
votes

You could bring another middle man into it, ie a bat script that launches ahk. Which could be launched through exec, here is an so post on launching bat

How do I run a batch file from my Java Application?

First try command line invocation of ahk passing the script but I'm not sure that will work, worth a try though