Hi Ameyrick, Thanks for your comments
I liked your idea regarding choosing the Python version, I wrote a slightly different version an built it into the RunPython() function as a second argument. Yours is possibly faster/more direct, but I thought it might be a bit neater to build it in, and allow for an auto-select of more python versions.
Code: Select all
;Example 1 - Run python code that is embedded as a string in autohotkey
py =
(
import ctypes
ctypes.windll.user32.MessageBoxW(0, "Brought to you by the knights who say ni", "Python Message Box", 1)
)
RunPython(py)
; OR, if you would like to specifically choose Python version 3.3
RunPython(py,"33")
return
;Example 2 - Run any selected python code with a hotkey (highlight some valid python code and then hit Ctrl + Win + S)
^#S:: RunPython()
RunPython(pyStr:="", PyVer:=""){ ; Requires the Selection() function included below
static PythonDll ; Set this Var as static so that repeated calls don't need to go through the PyVer discovery loop
if PyVer ; If PyVer has been Specified
{
if fileExist(A_WinDir "\SYSTEM32\python" PyVer ".dll")
PythonDll := A_WinDir "\SYSTEM32\python" PyVer ".dll"
else
return -1
}
if not fileExist(PythonDll)
{
Loop, 50 ; This is the PyVer discovery loop - Choose the highest version of Python available (up to 50)
{
if fileExist(A_WinDir "\SYSTEM32\python" (51 - A_Index) ".dll") ; This loop starts looking for python version 50 and works its way back to 1 (yes i know most of these don't even exist)
{
PythonDll := A_WinDir "\SYSTEM32\python" (51 - A_Index) ".dll"
break
}
if(A_Index = 50)
return -1
}
}
finalPyStr := (pyStr="") ? Selection() : pyStr ; if a string has been passed into this function then use that as the python code, otherwise use the currently selected text using my custom Selection() function
if(StrLen(finalPyStr) > 0){ ; Only do the following if there is some python code to run
DllCall("LoadLibrary", "Str", PythonDll)
DllCall(PythonDll "\Py_Initialize", "Cdecl")
DllCall(PythonDll "\PyRun_SimpleString", "AStr", finalPyStr)
DllCall(PythonDll "\Py_Finalize", "Cdecl")
}
return 1
}
;the below Selection() function is a dependency for RunPython() so that it can retrieve the contents of highlighted text
Selection(WaitTime:=0.5, ByRef TempClipSaved:="NotPassed"){
;returns the selected text. Optionally pass in the 'wait time' which prevents the action timing out for the defined amount of time (seconds).
;Optionally pass in a byref variable to TempClipSaved to hold the old clipboardAll data - to rewrite later. This will also stop the function from rewriting the original clipboard
;Alternatively simply change the TempClipSaved value to any arbitrary string to avoid rewriting the original clipboard
bolRewriteClip := (TempClipSaved="NotPassed") ? 1 : 0
TempClipSaved := ClipboardAll
Clipboard=
Send, ^c
ClipWait,%WaitTime%
TempSelection := (ErrorLevel)? "" : Clipboard
if(bolRewriteClip)
Clipboard := TempClipSaved
return, TempSelection
}
The reason why the popup is in Japanese text with python 2.7 is because I used the Unicode message box of the python ctypes library. In Python 2.7 you could use
ctypes.windll.user32.MessageBoxA(0, "Brought to you by the knights who say ni", "Python Message Box", 1) (Note the
MessageBoxA instead of the
MessageBoxW)
I know soooo little about Python or any other languages, and I've chosen Python and am desperately trying to learn it. I intend to use this little RunPython() function to help me get into it, since it will allow me to run short snippets of python code with a hotkey.