Running AHK with python
Hi all, i've looked around bit i can't seem to get any code i've found elsewhere working
I'm trying to execute an ahk script via python.
I've downloaded the <!-- m -->https://ahknet.autoh.../AutoHotkey.zip<!-- m --> but i'm not sure whether or not to use the files from the "Win32w" or "Win32a" folders. Similarly, can i just put all those files in the same folder as my scripts?
I'm using python 2.7
so i guess i'm asking, how do i run a .ahk file in the same folder as my python file, via python.
pythoncode.py:
import ctypes,time
import os, sys
ahk = ctypes.cdll.AutoHotkey
ahk.ahktextdll("") #start script in persistent mode (wait for action)
ahk.ahkdll("ahkTestScript.ahk","","")
ahkTestScript.ahk:
Run Notepad
edit: when i run via IDLE, i get the following error:
"WindowsError: exception: access violation reading 0x00000004"
at the line: ahk.ahktextdll("")
Any help would be appreciated.
转自: http://blog.csdn.net/tumin999/article/details/51206493
2016-04-21 10:53
目前使用ahk脚本(已编译成独立exe)来执行一个动作,而Python主程序负责调用这些不同的ahk程序。
以前是通过系统粘贴板来交互数据,即ahk程序启动后清除粘贴板,在结束时将执行情况已文字方式拷贝到粘贴板,python程序在等待ahk进程结束后再从粘贴板中读取执行结果。很显然这种方式有些弊端,比如不能同时执行多个ahk程序,还要避免人在电脑上进行ctrl-c ctrl-v的操作等。
研究了python的subprocess库,发现可以利用stdin stdout等来在进程间传递数据:
[python]
subprocess.check_output(args, *, input=None, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None)
Run command with arguments and return its output.
By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.
To also capture standard error in the result, use
stderr=subprocess.STDOUT
在AHK脚本中可以这样写回数据:
[ahk]
stdout := FileOpen("*", "w")
stdout.write(result)
stdout.close()
一个实际的例子:
[python]
ahk_res = subprocess.check_output([r"my_ahk.exe", "param1", "param2"])
print(ahk_res.decode("gbk"))