PageRenderTime 72ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ahkctypes.py

http://github.com/tinku99/ahkpy
Python | 95 lines | 65 code | 26 blank | 4 comment | 5 complexity | 82166497f49d7558894c4b72556e38a1 MD5 | raw file
  1. # by Irh9
  2. # http://www.autohotkey.com/forum/author-lrh9.html
  3. # http://www.autohotkey.com/forum/viewtopic.php?p=428795#428795
  4. import ctypes
  5. import itertools
  6. import os
  7. import sys
  8. ENCODING = 'utf-8'
  9. DLL_TYPES = ("Win32a", "Win32w", "X64W")
  10. DLL_NAMES = ("AutoHotkey.dll", "AutoHotkeyMini.dll")
  11. if __name__ == '__main__':
  12. _path = sys.argv[0]
  13. else:
  14. _path = __file__
  15. _dir = os.path.dirname(os.path.abspath(_path))
  16. DLL_PATHS = {}
  17. for dir_, name in itertools.product(DLL_TYPES, DLL_NAMES):
  18. DLL_PATHS[os.path.join(dir_, name)] = os.path.join(_dir, dir_, name)
  19. def set_library(dll_type, dll_name):
  20. global AHK
  21. AHK = ctypes.cdll.LoadLibrary(DLL_PATHS[os.path.join(dll_type, dll_name)])
  22. set_library('Win32a', 'AutoHotkey.dll')
  23. def _evaluate(obj):
  24. casted = ctypes.cast(obj, ctypes.c_char_p)
  25. value = casted.value
  26. # return eval(value.decode()) # not sure why we need eval here...
  27. return value.decode()
  28. def _represent(obj):
  29. return repr(obj).encode()
  30. def run(file_path, options="", parameters=""):
  31. return AHK.ahkdll(file_path.encode(), options.encode(), parameters.encode())
  32. def run_text(text="#Persistent\n#NoTrayIcon", options="", parameters=""):
  33. return AHK.ahktextdll(text.encode(), options.encode(), parameters.encode())
  34. def ready():
  35. return bool(AHK.ahkReady())
  36. def add(file_path, include_again=False, ignore_failure=0):
  37. return AHK.addFile(file_path.encode(), ctypes.c_uint8(include_again), ctypes.c_uint8(ignore_failure))
  38. def add_text(text, execute=0):
  39. return AHK.addScript(text.encode(), ctypes.c_uint8(execute))
  40. def execute_text(text):
  41. return AHK.ahkExec(text.encode())
  42. def execute_label(label, subroutine=True):
  43. nowait = not subroutine
  44. return bool(AHK.ahkLabel(label.encode(), ctypes.c_uint(nowait)))
  45. def execute_function(function, *args):
  46. p = AHK.ahkFunction(function.encode(), *[_represent(arg) for arg in args])
  47. return _evaluate(p)
  48. def post_function(function, *args):
  49. return not AHK.ahkPostFunction(function.encode(), *[_represent(arg) for arg in args])
  50. def assign(variable, value):
  51. return not AHK.ahkassign(variable.encode(), _represent(value))
  52. def get(variable, pointer=False):
  53. p = AHK.ahkgetvar(variable.encode(), ctypes.c_uint(pointer))
  54. return _evaluate(p)
  55. def terminate(kill=False):
  56. return not AHK.ahkTerminate(ctypes.c_int(kill))
  57. def reload():
  58. return not AHK.ahkReload()
  59. def get_function_pointer(function):
  60. return AHK.ahkFindFunc(function.encode())
  61. def get_label_pointer(label):
  62. return AHK.ahkFindLabel(label.encode())
  63. def pause():
  64. return bool(AHK.ahkPause("on".encode()))
  65. def unpause():
  66. return not AHK.ahkPause("off".encode())
  67. def execute_line(line_pointer, mode=0, wait=False):
  68. return AHK.ahkExecuteLine(ctypes.c_uint(line_pointer), ctypes.c_uint(mode), ctypes.c_uint(wait))