2016-04-15 311 views
2

我試圖在Python 2.7中運行autohotkey(ahk)腳本,但似乎沒有任何工作。我發現的所有在線資源不是過時就是過於複雜。如何在Python中運行autohotkey腳本(2.7)

有沒有人找到一種方法來做到這一點?我只想運行幾個簡單的腳本來激活窗口並打開應用程序。例如:

IfWinExist, Command Prompt - python ... 
    WinActivate 

更新:

我嘗試下載pyahk:

ahk.start() # Ititializes a new script thread 
ahk.ready() # Waits until status is True 
ahk.execute(mw._['cwd']+"winActivate_cmd.ahk") # Activate cmd window 

錯誤:無法加載autohotkey.dll

,並試着這樣:

import win32com.client # Import library/module 
dll = win32com.client.Dispatch("AutoHotkey.Script") #Creating DLL object? 
dll.ahktextdll() #no idea what this is doing...   
dll.ahkExec("WinActivate, Command Prompt - python") 

p wintypes.com_error無效類字符串

+0

我已經添加了這個作爲編輯 –

回答

0

好像你應該能夠使用subprocess剛剛推出的AutoHotkey的腳本作爲參數:

subprocess.call(["path/to/ahk.exe", "script.ahk"]) 

你不得不檢查AutoHotkey的文檔,但這個看起來像它應該工作。

+0

這工作!謝謝。沒想到會那麼簡單:)另外,事實證明我的.ahk文件實際上並沒有工作,但我只是在一個簡單的Hello World上嘗試過它。 –

+0

沒問題!奧卡姆的剃刀和所有這一切;有時最簡單的解決方案是最好的! –