Externally calling a function from running AHK script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sidlejinks
Posts: 5
Joined: 11 May 2018, 04:14

Externally calling a function from running AHK script

11 May 2018, 10:33

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)?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Externally calling a function from running AHK script

11 May 2018, 10:58

Maybe u could register a message that would dump the scripts vars, and post to it.
sidlejinks
Posts: 5
Joined: 11 May 2018, 04:14

Re: Externally calling a function from running AHK script

11 May 2018, 11:12

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.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Externally calling a function from running AHK script

11 May 2018, 11:25

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
sidlejinks
Posts: 5
Joined: 11 May 2018, 04:14

Re: Externally calling a function from running AHK script

11 May 2018, 12:01

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..
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Externally calling a function from running AHK script

14 May 2018, 11:42

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
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Externally calling a function from running AHK script

30 May 2018, 17:47

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.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, hiahkforum, ShatterCoder and 100 guests