Skip to content

Commit bb8dcfd

Browse files
committed
Implement setting the AutoHotkey executable path
1 parent 3ef3a2a commit bb8dcfd

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

ahkpy/launcher.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ def main():
1717
os.environ["PYTHONFULLPATH"] = ";".join(sys.path)
1818
os.environ["PYTHONDLL"] = python_dll_path()
1919

20-
ahk_exe_path = AHK
21-
ahk_assoc = get_ahk_by_assoc()
22-
if ahk_assoc and Path(ahk_assoc).name.lower().startswith("autohotkey"):
23-
ahk_exe_path = ahk_assoc
24-
ahk_exe_path = fix_ahk_platform(ahk_exe_path)
25-
20+
ahk_exe_path = fix_ahk_platform(get_ahk_exe_path())
2621
python_ahk_path = Path(__file__).parent / "Python.ahk"
2722
args = [str(ahk_exe_path), str(python_ahk_path)] + sys.argv[1:]
2823

@@ -57,6 +52,18 @@ def python_dll_path():
5752
return dllpath[:dllpath_len]
5853

5954

55+
def get_ahk_exe_path():
56+
env_path = os.getenv("AUTOHOTKEY")
57+
if env_path:
58+
return env_path
59+
60+
ahk_assoc = get_ahk_by_assoc()
61+
if ahk_assoc and Path(ahk_assoc).name.lower().startswith("autohotkey"):
62+
return ahk_assoc
63+
64+
return AHK
65+
66+
6067
def get_ahk_by_assoc():
6168
S_OK = 0
6269
S_FALSE = 1

docs/quickstart.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ The following CLI options are specific to AutoHotkey.py:
4646

4747
Don't show the AutoHotkey icon in the system tray.
4848

49+
When started, AutoHotkey.py searches for the AutoHotkey executable in the
50+
following sequence:
51+
52+
1. Check the ``AUTOHOTKEY`` environment variable. If the variable is set,
53+
treat it as the AutoHotkey executable path.
54+
2. Find the program associated with the ``*.ahk`` files. If the program name
55+
starts with ``autohotkey`` (case-insensitive), treat it as the AutoHotkey
56+
executable path.
57+
3. Use the default ``C:\Program Files\AutoHotkey\AutoHotkey.exe`` path.
58+
4959
.. note::
5060

5161
In contrast with AutoHotkey, the whole script is executed once it's loaded.

tests/test_cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,27 @@ def test_pyw(tmpdir):
196196
assert res.returncode == 0
197197

198198

199+
def test_ahk_path_envvar(tmpdir):
200+
spy = tmpdir / "spy.bat"
201+
code = "@echo %0 %*"
202+
spy.write(code)
203+
204+
script = tmpdir / "script.py"
205+
code = "print('hello')"
206+
script.write(code)
207+
208+
import os
209+
res = subprocess.run(
210+
["py.exe", "-m", "ahkpy", str(script)],
211+
env={**os.environ, "AUTOHOTKEY": str(spy)},
212+
encoding="utf-8",
213+
capture_output=True,
214+
)
215+
assert res.stderr == ""
216+
assert res.stdout.lower() == f"{spy} {ahk.__path__[0]}\\Python.ahk {script}\n".lower()
217+
assert res.returncode == 0
218+
219+
199220
def test_import_ahk(tmpdir):
200221
script = tmpdir / "script.py"
201222
code = "import ahkpy; print('ok')"

0 commit comments

Comments
 (0)