[AHK]python调用ahk,并读取ahk的执行结果

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



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值