I recently created a wrapper library for leveraging AutoHotkey from Python scripts.
https://github.com/spyoungtech/ahk
I know a lot of folks here are into Python and might appreciate this. There are already some existing tools for this, but they somewhat dated, unmaintained, and are for Python2 only, which is end-of-life this year.
This tool will work with modern versions of Python (3.6+).
It's not yet feature complete, but I feel there is enough implemented to be useful. Works with the latest version of AutoHotkey and plans to also support V2. Feedback appreciated.
I'm not very active here, so best way to give feedback is on GitHub or @Pythonic in the AHK discord server.
Using AutoHotkey from Python
Re: Using AutoHotkey from Python
@Pythonic, thanks for this! I have been experimenting with it, very impressive.
By the way, have you considered a gitter?
Regards,
burque505
Edit: added simple example (ahk script and python script)
By the way, have you considered a gitter?
Regards,
burque505
Edit: added simple example (ahk script and python script)
- Attachments
-
- AHK.zip
- (651 Bytes) Downloaded 1289 times
Last edited by burque505 on 19 Apr 2019, 11:28, edited 1 time in total.
Re: Using AutoHotkey from Python
@burque505, Thanks for checking it out and for including that sample.
That reminds me of a thought to include function to run a script from a file (that is, without the need to open it and read it first); maybe an optional argument for `run_script`.
I don't actively use gitter, but I may consider it if there's a demand for it.
Best,
Pythonic
That reminds me of a thought to include function to run a script from a file (that is, without the need to open it and read it first); maybe an optional argument for `run_script`.
I don't actively use gitter, but I may consider it if there's a demand for it.
Best,
Pythonic
Re: Using AutoHotkey from Python
@Pythonic, I like that idea a lot. I really look forward to using this.
Regards,
burque505
Regards,
burque505
Re: Using AutoHotkey from Python
@Pythonic
Thank you for your work!
I tried it also with burque505 two examples. Impressive.
Tested with IDLE 3.7 64bit
Thank you for your work!
I tried it also with burque505 two examples. Impressive.
Tested with IDLE 3.7 64bit
Re: Using AutoHotkey from Python
Awesome! I'd been putting off taking one of those and upgrading it to Python3 myself.
Re: Using AutoHotkey from Python
Hi,
I am getting this error bellow in Python.
How to fix thator add path to that script?
FileNotFoundError: [Errno 2] No such file or directory: 'guinea-pig.ahk'
I am getting this error bellow in Python.
How to fix thator add path to that script?
FileNotFoundError: [Errno 2] No such file or directory: 'guinea-pig.ahk'
Re: Using AutoHotkey from Python
@blue83, you would get that error from the script as written if you don't have 'guinea-pig.ahk' and 'python-ahk-test-5.py' in the same directory. If you do in fact have them in the same directory, you could try editing 'python-ahk-test-5.py' to provide a full path to 'guinea-pig.ahk'. (I assume the path to AutoHotkey.exe is correct.)
Regards,
burque505
Regards,
burque505
Re: Using AutoHotkey from Python
@burque505 yes, everything is in same directory, and path to Ahk is correct.
Here is new error after adding all the paths.
PS C:\Users\xxx> & C:/Users/xxx/AppData/Local/Continuum/miniconda3/python.exe c:/Users/xxx/Desktop/AHKPY/python-ahk-test-6.py
File "c:/Users/xxx/Desktop/AHKPY/python-ahk-test-6.py", line 3
ahk = AHK(executable_path="C:\Users\xxx\Desktop\xxx\Autohotkey\AutoHotkeyU64.exe")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
It says that part ="C is not correct.
Code: Select all
import time
from ahk import AHK
ahk = AHK(executable_path="C:\Users\xxx\Desktop\xxx\Autohotkey\AutoHotkeyU64.exe")
f = open("C:\Users\xxx\Desktop\AHKPY\guinea-pig.ahk", "r")
if f.mode == "r":
ahk_script = f.read()
print(ahk_script)
ahk.run_script(ahk_script, blocking=False)
PS C:\Users\xxx> & C:/Users/xxx/AppData/Local/Continuum/miniconda3/python.exe c:/Users/xxx/Desktop/AHKPY/python-ahk-test-6.py
File "c:/Users/xxx/Desktop/AHKPY/python-ahk-test-6.py", line 3
ahk = AHK(executable_path="C:\Users\xxx\Desktop\xxx\Autohotkey\AutoHotkeyU64.exe")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
It says that part ="C is not correct.
Re: Using AutoHotkey from Python
Of course, when I wrote last post my brain start to working and finally i have solution
Thank you anyway on your help and @Pythonic on this useful module.
Code: Select all
import time
from ahk import AHK
ahk = AHK(executable_path="C:\\Users\\xxx\\Desktop\\xxx\\Autohotkey\\AutoHotkeyU64.exe")
f = open("C:\\Users\\xxx\\Desktop\\AHKPY\\guinea-pig.ahk", "r")
if f.mode == "r":
ahk_script = f.read()
print(ahk_script)
ahk.run_script(ahk_script, blocking=False)
Re: Using AutoHotkey from Python
Here am I again.
One more question is, if I need to run first .ahk script and wait until ends, continue with python code and then execute another .ahk script and wait until finishes, how code will look like?
One more question is, if I need to run first .ahk script and wait until ends, continue with python code and then execute another .ahk script and wait until finishes, how code will look like?
Re: Using AutoHotkey from Python
@Pythonic, I just discovered that the version number is now at 0.8.1 and that lots of great new stuff has been added.
Thank you!
Regards,
burque505
Thank you!
Regards,
burque505
Re: Using AutoHotkey from Python
Hi,
How to catch variable from Python to Ahk?
Blue
How to catch variable from Python to Ahk?
Blue
Re: Using AutoHotkey from Python
Hi @blue83, one simple way (not using @Pythonic's ahk, though you could easily do so) is here, using RunCmd.ahk by @SKAN. Assumes 'varfrompy.py' is in the script directory. By the way, the ahk github page has examples for passing info from AHK to python.
Regards,
burque505
Code: Select all
#Include RunCmd.ahk
avar := RunCmd("python .\varfrompy.py")
msgbox %avar%
/* varfrompy.py:
myvar = "Hello, AHK!"
print(myvar)
*/
burque505
Re: Using AutoHotkey from Python
Hi @burque505 ,
Wow thank you for that, it will really change a lot of my scripts.
Thanks.
Wow thank you for that, it will really change a lot of my scripts.
Thanks.
Re: Using AutoHotkey from Python
:C My script seems to have VERY strange problems
Output:
Traceback (most recent call last):
File "C:\Users\Username\Desktop\Run ahk.pyw", line 8, in <module>
ahk.run_script(script.read, blocking = False)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\ahk\script.py", line 172, in run_script
result = self._run_script(script_text, decode=decode, blocking=blocking, **runkwargs)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\ahk\script.py", line 128, in _run_script
script_bytes = bytes(script_text, 'utf-8')
TypeError: encoding without a string argument[/code]
Code: Select all
from ahk import AHK as AutoHotkey
from tkinter import filedialog, Tk
w = Tk()
w.withdraw()
ahk = AutoHotkey(executable_path="C:\\Program Files\\AutoHotkey\\AutoHotkey.exe")
scriptfilepath = filedialog.askopenfilename(filetypes=[("AutoHotkey scripts", ".ahk")], title = "Open AutoHotkey script", parent = w)
script = open(scriptfilepath)
ahk.run_script(script.read, blocking = False)
Traceback (most recent call last):
File "C:\Users\Username\Desktop\Run ahk.pyw", line 8, in <module>
ahk.run_script(script.read, blocking = False)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\ahk\script.py", line 172, in run_script
result = self._run_script(script_text, decode=decode, blocking=blocking, **runkwargs)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\ahk\script.py", line 128, in _run_script
script_bytes = bytes(script_text, 'utf-8')
TypeError: encoding without a string argument[/code]
Re: Using AutoHotkey from Python
Hi @Coder436 sorry for the long delay in response. I am not as active here as I am on GitHub, but the issue you're seeing is an error in your script:Coder436 wrote: ↑18 Sep 2020, 11:11:C My script seems to have VERY strange problemsTypeError: encoding without a string argumentCode: Select all
ahk.run_script(script.read, blocking = False)
Code: Select all
script.read
Code: Select all
script.read()
Re: Using AutoHotkey from Python
Can do the inverse, using Python from AutoHotkey?
Re: Using AutoHotkey from Python
@songdg, there are a couple of ways at least that you can do that. See the ahk github page, my post here.
Regards,
burque505
Regards,
burque505
Return to “Scripts and Functions (v1)”
Who is online
Users browsing this forum: jacek678 and 176 guests