ahk icon indicating copy to clipboard operation
ahk copied to clipboard

Run script by specifying path to an ahk file

Open spyoungtech opened this issue 4 years ago • 7 comments

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.

spyoungtech avatar Dec 06 '19 01:12 spyoungtech

Is there a way to pass arguments to the script?

dcwong avatar Feb 02 '20 08:02 dcwong

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 avatar Feb 05 '20 03:02 spyoungtech

@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 !

ZepherusFF avatar Feb 28 '20 17:02 ZepherusFF

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)

spyoungtech avatar Feb 29 '20 07:02 spyoungtech

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')

thainq3127 avatar May 05 '20 02:05 thainq3127

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])

peterstavrou avatar May 25 '20 09:05 peterstavrou

Or you can simply do: ahk.run_script(open('script.ahk','r').read(os.path.getsize('script.ahk')))

digfish avatar Mar 04 '22 19:03 digfish

Implemented in v1. 🎉

spyoungtech avatar May 02 '23 05:05 spyoungtech