The Script Engine¶
The script
module, most essentially, houses the ScriptEngine
class.
The ScriptEngine
is responsible for rendering autohotkey code from jinja templates and executing that
code. This is the heart of how this package works. Every other major component either inherits from this class
or utilizes an instance of this class.
The current implementation of how autohotkey code is executed is by calling the autohotkey
executable with subprocess
.
-
ahk.script.
DEFAULT_EXECUTABLE_PATH
= C:\Program Files\AutoHotkey\AutoHotkey.exe¶ The deafult path to look for AutoHotkey, if not specified some other way
-
class
ahk.script.
AsyncScriptEngine
(executable_path='', directives=None, **kwargs)[source]¶ -
async
run_script
(script_text, decode=True, blocking=True, **runkwargs)¶ async version of
run_script
- Parameters
script_text (
str
) – a string containing AutoHotkey codedecode – If
True
, attempt to decode the stdout of the completed process. IfFalse
, returns the completed process. Only has effect whenblocking=True
blocking – If
True
, script must finish before returning. IfFalse
, function returns aasyncio.process
object immediately without blockingrunkwargs – keyword arguments passed to
subprocess.Popen
orsubprocess.run
- Returns
- A string of the decoded stdout if
blocking
anddecode
are True.A bytes object of stdout ifblocking
is True anddecode
is False.asyncio.subprocess.Process
object ifblocking
is False.
-
async
-
ahk.script.
DEFAULT_EXECUTABLE_PATH
= 'C:\\Program Files\\AutoHotkey\\AutoHotkey.exe' The deafult path to look for AutoHotkey, if not specified some other way
-
class
ahk.script.
ScriptEngine
(executable_path='', directives=None, **kwargs)[source]¶ -
__init__
(executable_path='', directives=None, **kwargs)[source]¶ This class is typically not used directly. AHK components inherit from this class and the arguments for this class should usually be passed in to
AHK
.- Parameters
executable_path (
str
) –the path to the AHK executable. If not provided explicitly in this argument, the path to the AHK executable is resolved in the following order:
The
AHK_PATH
environment variable, if presentDEFAULT_EXECUTABLE_PATH
if the file exists
If environment variable not present, tries to look for ‘AutoHotkey.exe’ or ‘AutoHotkeyA32.exe’ with shutil.which
directives (
Optional
[Set
]) – a set of directives to apply to all generated AHK scripts
- Raises
ExecutableNotFound – if AHK executable cannot be found or the specified file does not exist
-
async
a_run_script
(script_text, decode=True, blocking=True, **runkwargs)[source]¶ async version of
run_script
- Parameters
script_text (
str
) – a string containing AutoHotkey codedecode – If
True
, attempt to decode the stdout of the completed process. IfFalse
, returns the completed process. Only has effect whenblocking=True
blocking – If
True
, script must finish before returning. IfFalse
, function returns aasyncio.process
object immediately without blockingrunkwargs – keyword arguments passed to
subprocess.Popen
orsubprocess.run
- Returns
- A string of the decoded stdout if
blocking
anddecode
are True.A bytes object of stdout ifblocking
is True anddecode
is False.asyncio.subprocess.Process
object ifblocking
is False.
-
render_template
(template_name, directives=None, blocking=True, **kwargs)[source]¶ Renders a given jinja template and returns a string of script text
- Parameters
template_name – the name of the jinja template to render
directives – additional AHK directives to add to the resulting script
blocking – whether the template should be rendered to block (use #Persistent directive)
kwargs – keywords passed to template rendering
- Returns
An AutoHotkey script as a string
>>> from ahk import AHK >>> ahk = AHK() >>> ahk.render_template('keyboard/send_input.ahk', s='Hello') '#NoEnv\n#Persistent\n\n\nSendInput Hello\n\nExitApp\n'
-
run_script
(script_text, decode=True, blocking=True, **runkwargs)[source]¶ Given an AutoHotkey script as a string, execute it
- Parameters
script_text (
str
) – a string containing AutoHotkey codedecode – If
True
, attempt to decode the stdout of the completed process. IfFalse
, returns the completed process. Only has effect whenblocking=True
blocking – If
True
, script must finish before returning. IfFalse
, function returns asubprocess.Popen
object immediately without blockingrunkwargs – keyword arguments passed to
subprocess.Popen
orsubprocess.run
- Returns
- A string of the decoded stdout if
blocking
anddecode
are True.subprocess.CompletedProcess
ifblocking
is True anddecode
is False.subprocess.Popen
object ifblocking
is False.
>>> from ahk import AHK >>> ahk = AHK() >>> ahk.run_script('FileAppend, Hello World, *') 'Hello World' >>> ahk.run_script('FileAppend, Hello World, *', decode=False) CompletedProcess(args=['C:\\pathto\\AutoHotkey.exe', '/ErrorStdOut', '*'], returncode=0, stdout=b'Hello World', stderr=b'') >>> ahk.run_script('FileAppend, Hello World, *', blocking=False) <subprocess.Popen at 0x18a599cde10>
-