Run script by specifying path to an ahk file
Currently, the only way to run arbitrary autohotkey scripts is to use run_script, which only accepts a script as a string of the full script.
Functionality should be added to specify the path of an autohotkey script. Something like this should be possible
ahk.run_script('C:\\Path\\To\\MyScript.ahk')
I think this can safely/reasonably be added to the run_script method without issue.
Is there a way to pass arguments to the script?
Good question! Right now there's not an easy way to pass arguments to an AHK script. Something I can think of when implementing this feature.
I imagine it might take the form of a new keyword argument like script_args or something like that.
@spyoungtech :
I was trying to use this run_script command with a path and its not working. Here is what I get with the debug option :
DEBUG:ahk.script:Running script text: C:\Users\user.user\Desktop\ahk_scripts\Res.ahk
DEBUG:ahk.script:Stdout: b''
DEBUG:ahk.script:Stderr: b'C:\\Users\\user.user\\Desktop\\ahk_scripts\\* (1) : ==> This line does not contain a recognized action.\r\n Specifically: C:\\Users\\user.user\\Desktop\\ahk_scripts\\Res.ahk\r\n'
The .ahk file is your example :
#Persistent
data := "Hello Data!"
FileAppend, %data%, * ; send data var to stdout
ExitApp
Do you have any idea why it seems to do this ?
PS : Thanks alot for this project !
Hi @ZepherusFF this feature is not implemented yet, hence you get this error. Right now, run_script only supports a string containing a script :)
Right now you have to do
ahk = AHK()
with open('my_file.ahk') as file:
script_text = file.read()
ahk.run_script(script_text)
or
script = """
#Persistent
data := "Hello Data!"
FileAppend, %data%, * ; send data var to stdout
ExitApp
"""
ahk.run_script(script)
i cant use #include
CompletedProcess(args=['D:\\Programs\\AutoHotkey\\AutoHotkeyU64.exe', '/ErrorStdOut', '*'], returncode=2, stdout=b'', stderr=b'D:\\workspace\\auto\\* (7) : ==> Call to nonexistent function.\r\n Specifically: Gdip_Startup()\r\n')
I have tried to use the below but nothing executes
ahk = AHK()
with open('my_file.ahk') as file:
script_text = file.read()
ahk.run_script(script_text)
Update: I got around this issue by executing the script using subprocess
import subprocess
subprocess.call([C:\Program Files\AutoHotkey\AutoHotkey.exe, AHK_Script.ahk])
Or you can simply do:
ahk.run_script(open('script.ahk','r').read(os.path.getsize('script.ahk')))
Implemented in v1. 🎉