2

How can I run a python function from an AHK script? If it's possible, how would I:

  1. Pass arguments to the python function?
  2. Return data from the python function back to my running AHK script?

The only relevant information I could find was this answer, however I could not manage to implement it into my own script.

My best attempt at implementing this is the following snippet of code:

e::; nothing, because RunWait it's the command recommended in the question that I linked, so that means that I have to do a RunWait every time I press e?
Xiddoc
  • 3,369
  • 3
  • 11
  • 37
  • 1
    Please add your attempt to your question. – martineau May 21 '21 at 20:34
  • You don't call a Python function from ahk. You run a Python script. You can return a single number (the exit code), but not text data. What are you hoping to do? – Tim Roberts May 21 '21 at 20:43
  • I didn't know that, thanks...that's why I said if possible, because i'm not trying to return information(I was thinking to use that in a future project, but know I don't)....so youre saying that I cant call a python function from ahk? – adrian88888888 May 21 '21 at 20:51
  • If you want to not call runWait every time you run the script with new parameters, you can create a [`Function`](https://www.autohotkey.com/docs/Functions.htm) in ahk that takes the arguments as parameters and run the script with those parameters every time it is called. – Spyre May 21 '21 at 21:02
  • Additionally, while as @TimRoberts mentioned, the only thing that gets directly returned to AHK is the Exit Code of the Python program, you can alternatively have the Python Script write to a file, and then have your AHK script read the file once the Python Script is done executing. A potential downside is that this might take a few milliseconds of execution time every time it is called, so it may not be ideal if your program needs high levels of time precision or if you call the Python script several hundreds of times per second. If these conditions are fine, I can implement the code for you. – Spyre May 21 '21 at 21:06
  • _so youre saying that I cant call a python function from ahk?_ Not directly, but if you have your function in a script that can be called from a command line, that will work just fine. – Tim Roberts May 21 '21 at 21:08
  • While not the most applauded alternative, you could still think about using a file as bridge for data. – scso May 23 '21 at 10:48

1 Answers1

2

There is no native way to do this, as the only interaction AHK can do with Python is to run an entire script. However, you can modify the python script to accept arguments, then you can interact between the scripts (like the linked question).

The solution is as follows- similar to the question you linked, set up the python script so that it takes the function name and function parameters as arguments, then have it run the function with those arguments.

Similar to my answer on the linked question, you can use sys.argv to do this:

# Import the arguments from the sys module
from sys import argv

# If any arguments were passed (the first argument is the script name itself, so you must check for >1 instead of >0)
if len(argv) > 1:
    # This next line is basically saying- If argv is longer than 2 (the script name and function name)
    # Then set the args variable to everything other than the first 2 items in the argv array
    # Otherwise, set the args variable to None
    args = argv[2:] if len(argv) > 2 else None

    # If arguments were passed, then run the function (second item in argv) with the arguments (the star turns the list into args, or in other words turns the list into the parameter input format)
    # Otherwise, run the function without arguments
    argv[1](*args) if args else argv[1]()

# If there is code here, it will also execute. If you want to only run the function, then call the exit() function to quit the script.

Then, from AHK, all you would need to do is run the RunWait or Run command (depending on whether you want to wait for the function to finish) to call the function.

RunWait, script.py "functionName" "firstArgument" "secondArgument"

The second part of your question is tricky. In the question you linked, there is a nice explanation on how to return integer values (TLDR: use sys.exit(integer_value)), however if you want to return all sorts of data, like strings, for example, then the problem becomes more confusing. The thing is that at this point, I think the best solution is to write the output to a file, then have AHK read the file after the Python script is done executing. However, if you're already going to go down the "write to a file, then read it" route, then you might as well have already done that from the start and used that method to pass the function name and arguments to the python script.

Xiddoc
  • 3,369
  • 3
  • 11
  • 37