|
| 1 | +"""Native text reading via the UI Automation TextPattern. |
| 2 | +
|
| 3 | +``control_get_value`` reads a control through ValuePattern, but ValuePattern |
| 4 | +returns an **empty string** on multiline edits, RichEdit / document controls and |
| 5 | +web text areas — exactly the controls whose text you most want to read. UIA |
| 6 | +exposes that text through a different pattern, ``TextPattern``, which models the |
| 7 | +control's content as text ranges. ``ax_text`` adds three reads on top of the |
| 8 | +existing accessibility backend ABC: |
| 9 | +
|
| 10 | +* :func:`get_control_text` — the whole document's text (``DocumentRange``), |
| 11 | +* :func:`get_selected_text` — the currently selected text (``GetSelection``), |
| 12 | +* :func:`get_visible_text` — only the on-screen text (``GetVisibleRanges``). |
| 13 | +
|
| 14 | +Each function is a thin dispatch onto the injectable |
| 15 | +``accessibility.backends.get_backend()`` seam (the same seam the rest of the |
| 16 | +accessibility module uses), so the headless core is unit-testable on any |
| 17 | +platform by injecting a fake backend; the real UIA calls live in the Windows |
| 18 | +backend. Imports no ``PySide6``. |
| 19 | +""" |
| 20 | +from typing import Optional |
| 21 | + |
| 22 | + |
| 23 | +def _backend(): |
| 24 | + from je_auto_control.utils.accessibility.backends import get_backend |
| 25 | + return get_backend() |
| 26 | + |
| 27 | + |
| 28 | +def get_control_text(name: Optional[str] = None, role: Optional[str] = None, |
| 29 | + app_name: Optional[str] = None, |
| 30 | + automation_id: Optional[str] = None) -> Optional[str]: |
| 31 | + """Return a control's full text via TextPattern (``None`` if not found). |
| 32 | +
|
| 33 | + Unlike :func:`control_get_value`, this works on multiline edits, RichEdit / |
| 34 | + document controls and web text areas where ValuePattern returns ``""``. |
| 35 | + """ |
| 36 | + return _backend().document_text(name=name, role=role, app_name=app_name, |
| 37 | + automation_id=automation_id) |
| 38 | + |
| 39 | + |
| 40 | +def get_selected_text(name: Optional[str] = None, role: Optional[str] = None, |
| 41 | + app_name: Optional[str] = None, |
| 42 | + automation_id: Optional[str] = None) -> Optional[str]: |
| 43 | + """Return the control's currently selected text (TextPattern.GetSelection). |
| 44 | +
|
| 45 | + Empty string when nothing is selected; ``None`` if the control is not found |
| 46 | + or exposes no TextPattern. |
| 47 | + """ |
| 48 | + return _backend().selected_text(name=name, role=role, app_name=app_name, |
| 49 | + automation_id=automation_id) |
| 50 | + |
| 51 | + |
| 52 | +def get_visible_text(name: Optional[str] = None, role: Optional[str] = None, |
| 53 | + app_name: Optional[str] = None, |
| 54 | + automation_id: Optional[str] = None) -> Optional[str]: |
| 55 | + """Return only the on-screen text of a control (TextPattern.GetVisibleRanges). |
| 56 | +
|
| 57 | + Useful for scrolled documents where :func:`get_control_text` would return the |
| 58 | + whole (possibly huge) buffer. ``None`` if the control is not found. |
| 59 | + """ |
| 60 | + return _backend().visible_text(name=name, role=role, app_name=app_name, |
| 61 | + automation_id=automation_id) |
0 commit comments