diff --git a/README.md b/README.md index 288a7290..813e87ad 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,13 @@ A Python wrapper around AHK. +# Installation + +``` +pip install ahk +``` + + # Usage ```python @@ -11,11 +18,39 @@ ahk.mouse_move(x=100, y=100, speed=10) # blocks until mouse finishes moving print(ahk.mouse_position) # (100, 100) ``` -# Installation +![ahk](https://raw.githubusercontent.com/spyoungtech/ahk/master/docs/_static/ahk.gif) + +## non-blocking modes +You can also opt for a non-blocking interface, so you can do other stuff while AHK scripts run. + +```python +import time +from ahk import AHK +ahk = AHK() +ahk.mouse_position = (200, 200) # moves the mouse instantly to the position +start = time.time() +ahk.mouse_move(x=100, y=100, speed=30, blocking=False) +while True: # report mouse position while it moves + t = round(time.time() - start, 4) + position = ahk.mouse_position + print(t, position) + if position == (100, 100): + break ``` -pip install ahk + +You should see an output something like + ``` +0.032 (187, 187) +0.094 (173, 173) +0.137 (164, 164) +... +0.788 (100, 103) +0.831 (100, 101) +0.873 (100, 100) +``` + ## Dependencies @@ -36,4 +71,4 @@ Right now this is just an exploration of an idea. It may not even be a particula There's still a bit to be done in the way of implementation. -The vision is to provide additional interfaces that implement the most important parts of the AHK API in a Pythonic way. \ No newline at end of file +The vision is to provide additional interfaces that implement the most important parts of the AHK API in a Pythonic way. diff --git a/ahk/mouse.py b/ahk/mouse.py index c1a62143..842c02e9 100644 --- a/ahk/mouse.py +++ b/ahk/mouse.py @@ -39,7 +39,7 @@ def mouse_position(self, position): x, y = position self.mouse_move(x=x, y=y, speed=0, relative=False) - def _mouse_move(self, x=None, y=None, speed=None, relative=False, mode=None): + def _mouse_move(self, x=None, y=None, speed=None, relative=False, mode=None, persistent=True, blocking=True): if x is None and y is None: raise ValueError('Position argument(s) missing. Must provide x and/or y coordinates') if speed is None: @@ -61,9 +61,10 @@ def _mouse_move(self, x=None, y=None, speed=None, relative=False, mode=None): script = make_script(f''' CoordMode Mouse, {mode} MouseMove, {x}, {y} , {speed}{relative} - ''') + ''', persistent=persistent, blocking=blocking) return script def mouse_move(self, *args, **kwargs): + blocking = kwargs.get('blocking') script = self._mouse_move(*args, **kwargs) - self.run_script(script_text=script) + self.run_script(script, blocking=blocking) diff --git a/ahk/script.py b/ahk/script.py index 36850112..423446bd 100644 --- a/ahk/script.py +++ b/ahk/script.py @@ -4,7 +4,7 @@ from contextlib import suppress from shutil import which from ahk.utils import logger - +import time class ScriptEngine(object): def __init__(self, executable_path: str='', keep_scripts: bool=False, **kwargs): @@ -21,13 +21,19 @@ def __init__(self, executable_path: str='', keep_scripts: bool=False, **kwargs): self.executable_path = executable_path def _run_script(self, script_path, **kwargs): + blocking = kwargs.pop('blocking', True) runargs = [self.executable_path, script_path] decode = kwargs.pop('decode', False) - result = subprocess.run(runargs, stdin=None, stderr=None, stdout=subprocess.PIPE, **kwargs) - if decode: - return result.stdout.decode() + if blocking: + result = subprocess.run(runargs, stdin=None, stderr=None, stdout=subprocess.PIPE, **kwargs) + if decode: + return result.stdout.decode() + else: + return result.stdout else: - return result.stdout + p = subprocess.Popen(runargs, stdout=subprocess.PIPE, **kwargs) + p.stdout.readline() # give script a chance to read the script or else we'll delete it too quick + return p def run_script(self, script_text: str, delete=None, decode=True, **runkwargs): if delete is None: diff --git a/ahk/utils.py b/ahk/utils.py index 018b08a6..22d692da 100644 --- a/ahk/utils.py +++ b/ahk/utils.py @@ -14,7 +14,7 @@ logger.setLevel(logging.ERROR) -def make_script(body, directives=None, persistent=True): +def make_script(body, directives=None, persistent=True, blocking=True): """ Convenience function to dedent script body as well as add #Persistent directive and Exit/ExitApp :param body: body of the script @@ -39,4 +39,6 @@ def make_script(body, directives=None, persistent=True): {body} {exit_} ''') + if not blocking: + script = 'FileAppend, "`r`n", *\n' + script return script diff --git a/ahk/window.py b/ahk/window.py index a02c5389..b1b44f38 100644 --- a/ahk/window.py +++ b/ahk/window.py @@ -154,9 +154,9 @@ def _all_window_titles(self): WinGet windows, List Loop %windows% { - id := windows%A_Index% - WinGetTitle wt, ahk_id %id% - r .= wt . "`n" + id := windows%A_Index% + WinGetTitle wt, ahk_id %id% + r .= wt . "`n" } FileAppend, %r%, * ''') diff --git a/docs/_static/ahk.gif b/docs/_static/ahk.gif new file mode 100644 index 00000000..de24488e Binary files /dev/null and b/docs/_static/ahk.gif differ diff --git a/setup.py b/setup.py index 30185e53..b1ea062c 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='ahk', - version='0.1.1', + version='0.2.0', url='/~https://github.com/spyoungtech/ahk', description='A Python wrapper for AHK', author_email='spencer.young@spyoung.com',