1
- import collections
2
- import sys
3
1
from loguru import logger
4
2
import pywinauto
5
- from pywinauto import Desktop
6
- import time
7
3
from pprint import pprint
8
4
import pickle
9
5
@@ -82,21 +78,21 @@ def get_active_element_state(x: int, y: int):
82
78
"""
83
79
active_window = get_active_window ()
84
80
active_element = active_window .from_point (x , y )
85
- properties = active_element . get_properties ()
81
+ properties = get_properties (active_element )
86
82
properties ["rectangle" ] = dictify_rect (properties ["rectangle" ])
87
83
return properties
88
84
89
85
90
- def get_active_window (depth = 10 , max_width = 10 , filename = None ) -> Desktop :
86
+ def get_active_window () :
91
87
"""
92
88
Get the active window object.
93
89
94
90
Returns:
95
91
Desktop: The active window object.
96
92
"""
97
93
app = pywinauto .application .Application (backend = "uia" ).connect (active_only = True )
98
- window = app .active ()
99
- return window
94
+ window = app .top_window ()
95
+ return window . wrapper_object ()
100
96
101
97
102
98
def get_element_properties (element ):
@@ -120,8 +116,7 @@ def get_element_properties(element):
120
116
'children': [{'prop1': 'child_value1', 'prop2': 'child_value2',
121
117
'children': []}]}
122
118
"""
123
-
124
- properties = element .get_properties ()
119
+ properties = get_properties (element )
125
120
children = element .children ()
126
121
127
122
if children :
@@ -143,6 +138,37 @@ def dictify_rect(rect):
143
138
return rect_dict
144
139
145
140
141
+ def get_properties (element ):
142
+ """
143
+ Retrieves specific writable properties of an element.
144
+
145
+ This function retrieves a dictionary of writable properties for a given element.
146
+ It achieves this by temporarily modifying the class of the element object using
147
+ monkey patching.This approach is necessary because in some cases, the original
148
+ class of the element may have a `get_properties()` function that raises errors.
149
+
150
+ Args:
151
+ element: The element for which to retrieve writable properties.
152
+
153
+ Returns:
154
+ A dictionary containing the writable properties of the element,
155
+ with property names as keys and their corres
156
+ ponding values.
157
+
158
+ """
159
+ _element_class = element .__class__
160
+
161
+ class TempElement (element .__class__ ):
162
+ writable_props = pywinauto .base_wrapper .BaseWrapper .writable_props
163
+
164
+ # Instantiate the subclass
165
+ element .__class__ = TempElement
166
+ # Retrieve properties using get_properties()
167
+ properties = element .get_properties ()
168
+ element .__class__ = _element_class
169
+ return properties
170
+
171
+
146
172
def main ():
147
173
"""
148
174
Test function for retrieving and inspecting the state of the active window.
0 commit comments