diff --git a/openadapt/window/_windows.py b/openadapt/window/_windows.py index f56b1605a..40b74250b 100644 --- a/openadapt/window/_windows.py +++ b/openadapt/window/_windows.py @@ -1,9 +1,5 @@ -import collections -import sys from loguru import logger import pywinauto -from pywinauto import Desktop -import time from pprint import pprint import pickle @@ -82,12 +78,12 @@ def get_active_element_state(x: int, y: int): """ active_window = get_active_window() active_element = active_window.from_point(x, y) - properties = active_element.get_properties() + properties = get_properties(active_element) properties["rectangle"] = dictify_rect(properties["rectangle"]) return properties -def get_active_window(depth=10, max_width=10, filename=None) -> Desktop: +def get_active_window(): """ Get the active window object. @@ -95,8 +91,8 @@ def get_active_window(depth=10, max_width=10, filename=None) -> Desktop: Desktop: The active window object. """ app = pywinauto.application.Application(backend="uia").connect(active_only=True) - window = app.active() - return window + window = app.top_window() + return window.wrapper_object() def get_element_properties(element): @@ -120,8 +116,7 @@ def get_element_properties(element): 'children': [{'prop1': 'child_value1', 'prop2': 'child_value2', 'children': []}]} """ - - properties = element.get_properties() + properties = get_properties(element) children = element.children() if children: @@ -143,6 +138,37 @@ def dictify_rect(rect): return rect_dict +def get_properties(element): + """ + Retrieves specific writable properties of an element. + + This function retrieves a dictionary of writable properties for a given element. + It achieves this by temporarily modifying the class of the element object using + monkey patching.This approach is necessary because in some cases, the original + class of the element may have a `get_properties()` function that raises errors. + + Args: + element: The element for which to retrieve writable properties. + + Returns: + A dictionary containing the writable properties of the element, + with property names as keys and their corres + ponding values. + + """ + _element_class = element.__class__ + + class TempElement(element.__class__): + writable_props = pywinauto.base_wrapper.BaseWrapper.writable_props + + # Instantiate the subclass + element.__class__ = TempElement + # Retrieve properties using get_properties() + properties = element.get_properties() + element.__class__ = _element_class + return properties + + def main(): """ Test function for retrieving and inspecting the state of the active window.