| View previous topic :: View next topic |
| Author |
Message |
randallf
Joined: 06 Jul 2009 Posts: 678
|
Posted: Tue Jan 26, 2010 7:29 pm Post subject: AutoHotKey.dll with python |
|
|
Hello,
I am an AHK addict (basically all that I know at this point except some C) and I need to get into Python to go to the next level.
I simply do not have enough ability in Python to know how to use autohotkey.dll really at all - I cannot re-write everything that AHK can do in python either and I don't have the time to independently learn python - I need to combine the two in a way that lets me do everything I am already doing in AHK (for windows automation) in python so I can start to add python to what I'm doing...
Can anyone show me a very basic example of a python script that uses autohotkey.dll to activate a window using AHK's winactivate command? It would be helpful if this example showed how multiple parameters are passed.
Thank you very much! |
|
| Back to top |
|
 |
Murx Guest
|
Posted: Tue Jan 26, 2010 10:47 pm Post subject: |
|
|
| Minimum I would have expected that you link to that AutoHotkey.dll ... |
|
| Back to top |
|
 |
randallf
Joined: 06 Jul 2009 Posts: 678
|
|
| Back to top |
|
 |
trik
Joined: 15 Jul 2007 Posts: 1320
|
Posted: Tue Jan 26, 2010 11:26 pm Post subject: |
|
|
No, he means you would have to link it to your Python script. _________________ Religion is false. >_> |
|
| Back to top |
|
 |
randallf
Joined: 06 Jul 2009 Posts: 678
|
Posted: Tue Jan 26, 2010 11:34 pm Post subject: |
|
|
I understand what you mean, but I don't understand by what methods this is done.
>>>
command that includes library
CallToDLL (ahk command,?, ?, ?)
I am just looking for a super basic example that works to get started on.
ATM I don't know anything more than automating windows with AHK and basic concepts like functions/regex/expressions and some C stuff like reading hex and binary etc, the practical use of the advanced stuff gets over my head so I'm trying to find a familiar starting point that I can add from.
I figured this was a question that the right reader could answer in a few seconds, sorry for being so nub but I am just searching for a starting point.
Thanks for your time guys |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 513 Location: Houston, TX
|
Posted: Tue Jan 26, 2010 11:41 pm Post subject: embed python |
|
|
| look here |
|
| Back to top |
|
 |
randallf
Joined: 06 Jul 2009 Posts: 678
|
Posted: Wed Jan 27, 2010 2:30 pm Post subject: |
|
|
After loads of searching I found this blurb:
| Quote: | | If you are using C# or another .NET language, you are probably better off with IronAhk. This AutoHotkey.dll allows other programs to host AutoHotkey scripts, but does not (yet?) expose commands/functions directly to other languages. |
So to restate, what I am searching for is a way to execute AHK commands from a Python script. Is this possible? |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 513 Location: Houston, TX
|
Posted: Wed Jan 27, 2010 5:30 pm Post subject: embed autohotkey in python example |
|
|
| randallf wrote: | | I am searching for is a way to execute AHK commands from a Python script. Is this possible? |
Yes. Here is an example.
tested with python2.6, requires AutoHotkey.dll in the working directory or path...
ahkpython.py: | Code: | from ctypes import *
ahk = cdll.AutoHotkey
pyclient = create_string_buffer("ahkpython.ahk") # no unicode in ahk
CMPFUNC = CFUNCTYPE(c_int, c_int)
def py_cmp_func(a):
print "ahk: " , a
return a
cmp_func = CMPFUNC(py_cmp_func)
fx = create_string_buffer(str(cast(cmp_func, c_void_p).value))
script = create_string_buffer("""
fx2(msg){
WinActivate %msg%
msgbox in function fx2 with %msg% from python
return "success"
}
""")
ahk.ahkdll(pyclient, "", fx)
ahk.ahkassign(create_string_buffer("fx"), fx)
ahk.addScript(script)
ahk.ahkFunction(create_string_buffer("fx2"), create_string_buffer("Untitled"))
| ahkpython.ahk | Code: | #Persistent
dllcall(A_ScriptParams, "int", 42, "cdecl int")
return
f1::
inputbox, x, enter a numerical parameter for python callback
result := dllcall(A_ScriptParams, "int", x, "cdecl int")
return
|
remarks:
create_string_buffer is required because autohotkey.dll exported functions do not work with unicode.
See HotkeyIt's excellent chm help file for documentation on the functions. |
|
| Back to top |
|
 |
randallf
Joined: 06 Jul 2009 Posts: 678
|
Posted: Wed Jan 27, 2010 5:40 pm Post subject: |
|
|
T H A N K Y O U ! ! ! so much... this is what I was after. I have a lot to learn but now that I have the basics I believe have a resource for that.
Thanks, Tinku. Thanku. |
|
| Back to top |
|
 |
Chicken Pie 4 Tea
Joined: 18 Aug 2009 Posts: 375 Location: holland
|
Posted: Mon Nov 08, 2010 1:57 pm Post subject: |
|
|
could someone give a much simpler example?!
eg sending "hello world" to notepad using ahk via python script |
|
| Back to top |
|
 |
MacroMan!
Joined: 28 Aug 2009 Posts: 595 Location: Brighton, UK
|
Posted: Mon Nov 08, 2010 2:03 pm Post subject: |
|
|
Can you get any simpler than tinku99 wrote? _________________ I'm still here. |
|
| Back to top |
|
 |
a4u Guest
|
Posted: Mon Nov 08, 2010 2:06 pm Post subject: |
|
|
| The AutoHotkey.dll now has a COM interface. I'd recommend using that if you want a simpler way to work with the DLL in python. |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
|
| Back to top |
|
 |
randallf
Joined: 06 Jul 2009 Posts: 678
|
Posted: Mon Nov 08, 2010 6:38 pm Post subject: |
|
|
| HotKeyIt wrote: | Here are some more non COM examples  |
Thank you! I haven't got back to this yet as my job led me more to VBA but I definitely am coming back here at some point. |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 513 Location: Houston, TX
|
Posted: Tue Dec 28, 2010 10:51 pm Post subject: python autohotkey com example |
|
|
| a4u wrote: | | The AutoHotkey.dll now has a COM interface. I'd recommend using that if you want a simpler way to work with the DLL in python. | Here you go: | Code: | import win32com.client
dll = win32com.client.Dispatch("AutoHotkey.Script")
dll.ahktextdll()
dll.ahkExec("a=_Hello World!")
dll.ahkExec("StringTrimLeft,a,a,1")
dll.ahkgetvar("a")
dll.ahkExec("msgbox " + dll.ahkgetvar("a")) |
requires: regsvr32.exe c:\pathtoahkdll\Win32w\AutoHotkey.dll
requires: pythonwin32, i use pythonxy
reference
@HotkeyIt:
1. can you provide the type library for autohotkey com interface as well ?
2. Is it possible to return safearrays (ComObjArray) using ahkgetvar(varname) ?
So we can do something like this from python: | Code: | code = '''
arr := ComObjArray(VT_VARIANT:=12, 3)
arr[0] := "Auto"
arr[1] := "Hot"
arr[2] := "key"
'''
dll.ahkExec(code)
dll.ahkgetvar("arr") |
|
|
| Back to top |
|
 |
|