Page 1 of 1

Externally calling a function from running AHK script

Posted: 11 May 2018, 10:33
by sidlejinks
Hi everybody!

I really need some help and I ask for your kind advice.

I have a java program and I want to make a function call from it into the running ahk script. For example running ahk script has some dynamic data that I want to retrieve and use in java program.
I know how to use ahk functions with a passive ahk script, but I have no idea how to interact with an active ahk script.

For testing purposes I made a simple ahk (count.ahk):

Code: Select all

global count
count = 10

^g::
{
returnvalue()
return
}

^t::
{
count++
return
}

returnvalue(){
Msgbox count=%count%
return
}
Java testing code looks like this:

Code: Select all

import AHK.AhkDllController;
import com.sun.jna.Native;
import com.sun.jna.WString;

public class Test{
    public static void main(String args[]) {

        AhkDllController.autoHotKeyDll lib =  Native.loadLibrary("AutoHotkey", AhkDllController.autoHotKeyDll.class);

        lib.ahktextdll(new WString(""));
        lib.addFile(new WString(System.getProperty("user.dir") + "\\count.ahk"), 1, 0);
        lib.ahkFunction(new WString("returnvalue"));
    }
}
So after executing this java code I get Msgbox with "count=10" text.

What should I do to be able to get "count" value from a running ahk (preferably not a compiled one)?

Re: Externally calling a function from running AHK script

Posted: 11 May 2018, 10:58
by swagfag
Maybe u could register a message that would dump the scripts vars, and post to it.

Re: Externally calling a function from running AHK script

Posted: 11 May 2018, 11:12
by sidlejinks
Well sounds good, but I'm afraid that's not enough in my case. I also need to call an ahk function with certain parameters from java program and get it's output.

Re: Externally calling a function from running AHK script

Posted: 11 May 2018, 11:25
by swagfag
i fail to see how that precludes you from using messages. have the script respond to a message, carry out the function and post the results back to your java app

Re: Externally calling a function from running AHK script

Posted: 11 May 2018, 12:01
by sidlejinks
swagfag wrote:i fail to see how that precludes you from using messages. have the script respond to a message, carry out the function and post the results back to your java app
It seems it could work. Can you please give some details or example how to do this? I don't know AHK that good..

Re: Externally calling a function from running AHK script

Posted: 11 May 2018, 12:16
by swagfag

Re: Externally calling a function from running AHK script

Posted: 11 May 2018, 12:48
by sidlejinks
Alright thanks a lot man!

Re: Externally calling a function from running AHK script

Posted: 14 May 2018, 11:42
by FanaticGuru
You can use ObjRegisterActive linked below:
https://autohotkey.com/boards/viewtopic.php?t=6148

This will allow you to register an AutoHotkey object with Windows as a COM object. Then any program that can access COM objects (most popular Windows programming languages can) will be able to access the methods and properties exposed by the AutoHotkey script. Methods are basically functions, properties are basically variables.

You will be able to access the AutoHotkey script the same way you would access Excel and Word from another program.

FG

Re: Externally calling a function from running AHK script

Posted: 30 May 2018, 17:47
by GeekDude
Another option would be to use my library RemoteObj.ahk to expose a socket-based API. You can connect to the socket using Java and send a little bit of JSON to invoke the API.

https://github.com/G33kDude/RemoteObj.ahk

e.g. if you expose an AHK class instance called Tools which has a method ReturnValue, you could send the JSON {"Action": "__Call", "Name": "Return, "Params": []} to invoke the API. In response you'll receive a JSON string {"RetVal": MethodReturnValue} where MethodReturnValue is the value returned by that method.