Using AutoHotkey from Python

Post your working scripts, libraries and tools
Pythonic
Posts: 3
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: 1371
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 455 times
Last edited by burque505 on 19 Apr 2019, 11:28, edited 1 time in total.
Pythonic
Posts: 3
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: 1371
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: 49
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: 125
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: 1371
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: 125
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: 125
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: 125
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?
burque505
Posts: 1371
Joined: 22 Jan 2017, 19:37

Re: Using AutoHotkey from Python

16 Apr 2020, 14:55

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

Re: Using AutoHotkey from Python

25 Aug 2020, 05:37

Hi,

How to catch variable from Python to Ahk?

Blue
burque505
Posts: 1371
Joined: 22 Jan 2017, 19:37

Re: Using AutoHotkey from Python

28 Aug 2020, 10:48

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.

Code: Select all

#Include RunCmd.ahk

avar := RunCmd("python .\varfrompy.py")
msgbox %avar%

/* varfrompy.py:

myvar = "Hello, AHK!"
print(myvar)
*/
Regards,
burque505
blue83
Posts: 125
Joined: 11 Apr 2018, 06:38

Re: Using AutoHotkey from Python

28 Aug 2020, 12:47

Hi @burque505 ,

Wow thank you for that, it will really change a lot of my scripts.

Thanks.
Coder436
Posts: 17
Joined: 02 Apr 2020, 13:26

Re: Using AutoHotkey from Python

18 Sep 2020, 11:11

:C My script seems to have VERY strange problems

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)
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]
Pythonic
Posts: 3
Joined: 21 Mar 2019, 15:06
GitHub: spyoungtech

Re: Using AutoHotkey from Python

15 Oct 2020, 03:58

Coder436 wrote:
18 Sep 2020, 11:11
:C My script seems to have VERY strange problems

Code: Select all

ahk.run_script(script.read, blocking = False)
TypeError: encoding without a string argument
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:

Code: Select all

script.read
should instead be

Code: Select all

script.read()
Hope that helps :D
songdg
Posts: 166
Joined: 04 Oct 2017, 20:04

Re: Using AutoHotkey from Python

16 Oct 2020, 08:54

Can do the inverse, using Python from AutoHotkey?
burque505
Posts: 1371
Joined: 22 Jan 2017, 19:37

Re: Using AutoHotkey from Python

16 Oct 2020, 17:14

@songdg, there are a couple of ways at least that you can do that. See the ahk github page, my post here.
Regards,
burque505

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Flipeador, Google [Bot], leosouza85 and 20 guests