Using AutoHotkey from Python

Post your working scripts, libraries and tools for AHK v1.1 and older
songdg
Posts: 628
Joined: 04 Oct 2017, 20:04

Re: Using AutoHotkey from Python

16 Oct 2020, 22:49

burque505 wrote:
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
Thanks for your help :D
chango
Posts: 30
Joined: 27 May 2017, 12:10

Re: Using AutoHotkey from Python

08 Nov 2020, 07:06

I'm shifting to a combination of AHK and python atm so I'll watch this thread
blue83
Posts: 157
Joined: 11 Apr 2018, 06:38

Re: Using AutoHotkey from Python

07 Jan 2021, 06:52

burque505 wrote:
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
Hi,

Your code is great for normal purposes.

But I have also a code with python on different path and this code bellow doesnt work, can you help me?
What I am doing wrong?

Code: Select all

avar := RunCmd("C:/Users/%A_UserName%/Desktop/WPy64-3760/python-3.7.6.amd64/python.exe %A_ScriptDir%/varfrompy.py", A_ScriptDir, "UTF-8")
msgbox %avar%
I have found it.

Code: Select all

avar := RunCmd("C:/Users/" A_UserName "/Desktop/WPy64-3760/python-3.7.6.amd64/python.exe .\varfrompy.py", A_ScriptDir, "UTF-8")
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Using AutoHotkey from Python

15 Feb 2023, 13:57

I am really liking @Pythonic's ahk lib! I've been creating some AHK (v1) GUIs that pass dictionaries and lists to Python. I soon discovered I need to massage them into a string to have Python read them using ahk.py.

I started off with obj2str.ahk by @errorseven (many thanks!) but it choked for my purposes on multiline strings and other things. My mod of obj2str (Obj2PyStr.ahk) is below.
(Tested with Python 3.10.4 on Windows 10 Pro 64-bit only, latest ahk.py)
Spoiler
There are examples for use in the script, and at the obj2pystr GitHub page.
One for dictionaries (i.e. associative arrays in AHK) is below.
AHK:

Code: Select all

#Include obj2pystr.ahk

hughes = 
(
John James Hughes
as Trusteee
of the J.J. Hughes Revocable Living Trust
)

MyDict := {borrowers:[{1_FullName:"James Roberts", 2_Street:"346 Cage St", 3_City:"Tarzana", 4_State:"CA", 5_Zip:90111}]
, lenders:[{1_FullName:hughes, 2_Street:"123 Anywhere", 3_City:"Albacore", 4_State:"NT", 5_Zip:80111}]}

;MsgBox % Obj2PyStr(MyDict)
FileAppend, % Obj2PyStr(MyDict), *
Python to process that:

Code: Select all

from ahk import AHK
from ast import literal_eval

ahk = AHK()

with open(r"your_script_here4.ahk", 'r') as f:
    my_script =  f.read()

result = ahk.run_script(my_script)
print(result)  
NewStuff = literal_eval(result)
print(type(NewStuff))
print(NewStuff["lenders"][0]["1_FullName"])
Result:

Code: Select all

{"borrowers":[{"1_FullName":'''James Roberts''', "2_Street":'''346 Cage St''', "3_City":'''Tarzana''', "4_State":'''CA''', "5_Zip":90111}], "lenders":[{"1_FullName":'''John James Hughes
as Trusteee
of the J.J. Hughes Revocable Living Trust''', "2_Street":'''123 Anywhere''', "3_City":'''Albacore''', "4_State":'''NT''', "5_Zip":80111}]}
<class 'dict'>
John James Hughes
as Trusteee
of the J.J. Hughes Revocable Living Trust
NOTE: The Python creates a really ugly object because of the single quotes and triple single quotes, but I ended up doing it that way so ast.literal_eval() can process the object.

Regards,
burque505
songdg
Posts: 628
Joined: 04 Oct 2017, 20:04

Re: Using AutoHotkey from Python

18 May 2023, 02:28

How to use it in Anaconda with spyder, I place the library(ahk_main) in the directory(C:\ProgramData\Anaconda3\Lib\site-packages) that I can use the code import ahk_main, but when I try ahk = AHK() it doesn't work.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 218 guests