Using AutoHotkey from Python

Post your working scripts, libraries and tools
Pythonic
Posts: 2
Joined: 21 Mar 2019, 15:06
GitHub: spyoungtech

Using AutoHotkey from Python

28 Mar 2019, 14:36

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.
burque505
Posts: 1135
Joined: 22 Jan 2017, 19:37

Re: Using AutoHotkey from Python

28 Mar 2019, 14:47

@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)
Attachments
AHK.zip
(651 Bytes) Downloaded 178 times
Last edited by burque505 on 19 Apr 2019, 11:28, edited 1 time in total.
Pythonic
Posts: 2
Joined: 21 Mar 2019, 15:06
GitHub: spyoungtech

Re: Using AutoHotkey from Python

28 Mar 2019, 15:14

@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
burque505
Posts: 1135
Joined: 22 Jan 2017, 19:37

Re: Using AutoHotkey from Python

28 Mar 2019, 15:44

@Pythonic, I like that idea a lot. I really look forward to using this.
Regards,
burque505
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Using AutoHotkey from Python

28 Mar 2019, 16:20

@Pythonic
Thank you for your work! :bravo: :salute:
I tried it also with burque505 two examples. Impressive.
Tested with IDLE 3.7 64bit
mshafer1
Posts: 32
Joined: 16 Jul 2017, 14:49
GitHub: mshafer1
Contact:

Re: Using AutoHotkey from Python

28 Mar 2019, 19:12

Awesome! I'd been putting off taking one of those and upgrading it to Python3 myself.
blue83
Posts: 71
Joined: 11 Apr 2018, 06:38

Re: Using AutoHotkey from Python

20 Nov 2019, 17:48

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'
burque505
Posts: 1135
Joined: 22 Jan 2017, 19:37

Re: Using AutoHotkey from Python

20 Nov 2019, 18:20

@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
blue83
Posts: 71
Joined: 11 Apr 2018, 06:38

Re: Using AutoHotkey from Python

21 Nov 2019, 02:44

@burque505 yes, everything is in same directory, and path to Ahk is 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)
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.
blue83
Posts: 71
Joined: 11 Apr 2018, 06:38

Re: Using AutoHotkey from Python

21 Nov 2019, 02:57

Of course, when I wrote last post my brain start to working and finally i have solution :)

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)
Thank you anyway on your help and @Pythonic on this useful module.
blue83
Posts: 71
Joined: 11 Apr 2018, 06:38

Re: Using AutoHotkey from Python

21 Nov 2019, 03:20

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?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: UnReALiTyy and 65 guests