Python and AutoHotKey

Discuss other programming languages besides AutoHotkey
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Python and AutoHotKey

Post by Joe Glines » 26 May 2014, 20:56

I saw this code https://code.google.com/p/pahk/ which allows you to use Python in AHK and I found a few posts on the other AutoHotKey forum like this one talking about using the Autohotkey.dll file http://www.autohotkey.com/board/topic/3 ... ntry646062

I've successfully created an AHK script and used Python in it as well as created a Python script using AutoHotKey but was curious if anybody has any examples they could share. Specifically one thing I have not been able to do is to have a Python program use a hotstring from AutoHotKey.

thanks!
Joe
User avatar
joedf
Posts: 6486
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: Python and AutoHotKey

Post by joedf » 26 May 2014, 22:55

Interesting... I havent tried this yet... Any code samples you mind sharing?
For Hotstrings... What have you tried?

Regards
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 29 May 2014, 12:57

I have several that I will post. Just been swamped at work! I'll get to it this weekend.
Regards,
Joe
User avatar
joedf
Posts: 6486
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: Python and AutoHotKey

Post by joedf » 29 May 2014, 20:59

Sure, take your time! :)
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 07 Jun 2014, 06:18

Sorry for the delay in posting. Here is an example using AutoHotKey within Python

# Two examples using AutoHotKey from within Python
import win32com.client # Import library / module
dll = win32com.client.Dispatch("AutoHotkey.Script") #Creating DLL object?
dll.ahktextdll() #no idea what this is doing...

dll.ahkExec("Send,My title`n") #uses AutoHotKey to send keystrokes from within Python
dll.ahkExec("Send,second line") #same as above just giving the example twice

#~ Second example- showing a bit more cool things
dll.ahkExec("Send,^a") #Use AutoHotKey to send Control+A (select all)
dll.ahkExec("Send,^c") #use AutoHotKey to copy selected text
dll.ahkExec("Sleep, 1000") #calling sleep in AutoHotKey for 1 second
dll.ahkExec("msgbox %Clipboard%") #using AutoHotKey to show a Msgbox of the clipboard
dll.ahkExec("Click right 144, 155") #using AutoHotKeyto have a right click at specific location
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 07 Jun 2014, 06:52

Here is another exmaple using AutoHotKey within a Python Script. My examples are showing concepts- I don't really use the below, just need the abilities

import win32com.client #Import Library
ahk = win32com.client.Dispatch("AutoHotkey.Script") # I'm not sure what this does and why I need it here and not in my other script
ahk.ahktextdll("","","") # again- no idea what this is
ahk.ahkExec("#t::Run B:\Diet.xls") # Win+T opens a file on my computer
# When the folliwing line is commented out, the below hotkey runs w/o pressing it, but then it works correctly. When I don't comment it out, the above hotkey will not work. I'd love to know how to have multiple hotkeys within one Python script
ahk.ahktextdll("","","")
ahk.ahkExec("^t::Run http://www.google.com") #Cntrl+t launches default browser to google

ahk.ahkExec("while 1\nSleep 100") #This (infininte loop) keeps script running- needs to be after the hotkey
#~ ahk.ahktextdll('#Persistent') #This doesn't keep it running as I'd hoped
#~ ahk.ahkExec('#Persistent') #This doesn't keep it running as I'd hoped
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 07 Jun 2014, 07:03

I found this script Python script here: https://code.google.com/p/pahk/
Which will use AutoHotKey to copy the clipoard's contect to a Python variable.

from pahk import Interpreter
from time import sleep

#this script definds a function that uses AutoHotKey to copy the clipboard's content
def get_clipboard():
ahk_interpreter = Interpreter()
ahk_script = '''#Persistent\nx = %clipboard%''' # Create a variable "x" which hold the clipboard data.
ahk_interpreter.execute_script(ahk_script)
sleep(0.5) #Let the thread warms up

clipboard = ahk_interpreter.var_get('x') #Get the value of the variable x in the current running script

ahk_interpreter.terminate() # Terminate the running script
del ahk_interpreter

return clipboard

print (get_clipboard())
sleep(30)
HotKeyIt
Posts: 1696
Joined: 29 Sep 2013, 18:35
Contact:

Re: Python and AutoHotKey

Post by HotKeyIt » 07 Jun 2014, 07:09

This example uses AutoHotkey.dll COM Interface instead of loading dll and defining functions.
Note that the dll must be registred using regsvr32 to be able to load the dll via COM.

Code: Select all

import win32com.client # Import Library
import time # for time.sleep
ahk = win32com.client.Dispatch("AutoHotkey.Script") # same as ahk := ComObjCreate("AutoHotkey.Script")
ahk.ahktextdll("","","") # start an empty '#Persistent' script
# ahk.ahkExec("#t::Run B:\Diet.xls") # this code is not good since ahkExec is used to execute commands and not add code
ahk.addScript("#t::Run B:\Diet.xls") # add code to running script
# ahk.ahktextdll("","","") # this would exit previous script and start a new one (#Persistent).
ahk.addScript("^t::Run http://www.google.com") # add more code to running script

while true
  time.sleep(10000)
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 07 Jun 2014, 07:14

Here is an example that I found here: http://www.autohotkey.com/board/topic/4 ... ntry390670
which runs Python from within AutoHotKey. (I get an error when I click the hotkey but Python does grab the computer name)

#SingleInstance, Force
^t::
i := python := ComObjCreate("Python.Interpreter")
i.Exec("import win32api")
r := i.Eval("win32api.GetComputerName()")
msgbox % r
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 07 Jun 2014, 07:35

HotKeyIt- thank you for your input! I'm completely out of my league here and your help is very much appreciated!
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 07 Jun 2014, 08:22

BTW- I had to update the while true statement to:

while 1:
time.sleep(10000)

I believe I needed the colon for Python and while I think "while true:" works with Python 3.x, it doesn't work for me in Python 2.7
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: Python and AutoHotKey

Post by guest3456 » 18 Jun 2014, 21:14

Joetazz are you very experienced with Python?

User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 19 Jun 2014, 07:18

Absolutely not! I'm learning it because "nobody" knows what AutoHotKey is thus it doesn't do any good having it on my resume. Even though everything I've done in Python I can do in AutoHotKey... The only really cool thing I did in Python that I haven't done in AutoHotKey is multi-threaded web scraping.
HotKeyIt
Posts: 1696
Joined: 29 Sep 2013, 18:35
Contact:

Re: Python and AutoHotKey

Post by HotKeyIt » 02 Feb 2016, 15:36

Joe Glines wrote:The only really cool thing I did in Python that I haven't done in AutoHotKey is multi-threaded web scraping.
Multi-Threading in AHK_H is very simple, it works by using multiple AutoHotkey.dll threads:

Code: Select all

Loop 3
  dll%A_Index%:=AhkThread("Alias(download," getvar(download%A_Index%) ")`ndownload:=URLDownloadToVar(""https://www.google.de/?gws_rd=ssl#q=autohotkey" A_Index """)`n" CreateScript("URLDownloadToVar{}"))
While dll1.ahkReady() || dll3.ahkReady() || dll3.ahkReady()
  Sleep 100
loop 3
  Gui,Add,Edit,w480 h60,% download%A_Index%
Gui,Show
return
GuiClose:
ExitApp

URLDownloadToVar(url){
	hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
	hObject.Open("GET",url)
	hObject.Send()
	return hObject.ResponseText
}
User avatar
Joe Glines
Posts: 555
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Python and AutoHotKey

Post by Joe Glines » 02 Feb 2016, 16:30

@HotkeyIt Thanks! I'm a bit swamped at the moment but I look forward to checking this out!
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Python and AutoHotKey

Post by BoBo » 08 Oct 2017, 11:30

Google Calendar Command Line Interface

gcalcli is a Python application that allows you to access your Google Calendar(s) from a command line. It's easy to get your agenda, search for events, add new events, delete events, edit events, and even import those annoying ICS/vCal invites from Microsoft Exchange and/or other sources. Additionally, gcalcli can be used as a reminder service and execute any application you want when an event is coming up.


... just in case someone is looking for a challenge eg. to create an AHK wrapper based on the above mentioned options :thumbup: (c't 21/2017, Seite/page 156)

https://github.com/insanum/gcalcli
Post Reply

Return to “Other Programming Languages”