Changeset 23
- Timestamp:
- 11/19/07 16:50:40 (12 months ago)
- Files:
-
- 1 modified
-
collected/trunk/python-msaa/walk.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
collected/trunk/python-msaa/walk.py
r22 r23 1 # walk.py 2 # 3 # A Python program that walks through the MSAA hierarchy, printing 4 # information about each accessible object that it finds. 5 # 6 # Requirements: Python and comtypes 7 # 8 # (tested with Python 2.5 and comtypes 0.4.0 on Windows XP) 9 # 10 # comtypes 11 # http://sourceforge.net/projects/comtypes/ 12 # 13 # Microsoft Active Accessibility 14 # http://msdn2.microsoft.com/en-us/library/ms697707.aspx 15 # 16 # Active Accessibility Client Interfaces and Functions 17 # http://msdn2.microsoft.com/en-us/library/ms696161.aspx 18 # 19 # IAccessible 20 # http://msdn2.microsoft.com/en-us/library/ms696165.aspx 21 1 22 from ctypes import windll, oledll, c_long, byref, POINTER, WINFUNCTYPE 2 23 from ctypes.wintypes import HWND, LPARAM … … 6 27 from comtypes.gen.Accessibility import IAccessible 7 28 29 # some MSAA constants 8 30 OBJID_WINDOW = 0 9 31 CHILDID_SELF = 0 … … 76 98 } 77 99 78 step = 479 80 100 prototype = WINFUNCTYPE(None, HWND, LPARAM) 81 101 102 # passed to windll.user32.EnumWindows below to be called on each window 82 103 def callback(hwnd, lParam): 104 """retrieve the IAccessible interface for the window hwnd and inspect it""" 83 105 ppvObject = POINTER(IAccessible)() 84 106 oledll.oleacc.AccessibleObjectFromWindow(hwnd, OBJID_WINDOW, … … 86 108 inspect(ppvObject) 87 109 88 def inspect(ia, indent=0): 89 varID = VARIANT(CHILDID_SELF) 90 accName = ia.accName[varID] 110 def inspect(ia, indent=0, indent_step=4): 111 """print out the name, role, and number of children for the 112 IAccessible ia and each of its children""" 113 # retrieve the name, role, and number of children for ia 114 selfID = VARIANT(CHILDID_SELF) 115 accName = ia.accName[selfID] 91 116 if accName == None or accName == '': 92 117 accName = 'NAMELESS' 93 118 else: 94 119 accName = unicode(accName).encode('utf_8') 95 accRole = ia.accRole[ varID]120 accRole = ia.accRole[selfID] 96 121 if accRole in roles: 97 122 accRoleText = roles[accRole] … … 101 126 print '%s%s (%s, %d children)' % (' ' * indent, accName, accRoleText, accChildCount) 102 127 128 # recurse through the accessible children 103 129 if accChildCount > 0: 130 # allocate memory for information about the children 131 # (VARIANT structures) 104 132 VariantArrayType = VARIANT * accChildCount 105 133 rgvarChildren = VariantArrayType() … … 107 135 oledll.oleacc.AccessibleChildren(ia, 0, accChildCount, rgvarChildren, 108 136 byref(pcObtained)) 137 # child.vt may be VT_I4 or VT_DISPATCH 138 # see the MSDN documentation on the AccessibleChildren function 139 # http://msdn2.microsoft.com/en-us/library/ms697243.aspx 109 140 for child in rgvarChildren: 110 141 if child.vt == VT_I4: … … 112 143 ppdispChild = ia.accChild(child) 113 144 if ppdispChild: 114 inspect(ppdispChild.QueryInterface(IAccessible), indent + step) 145 inspect(ppdispChild.QueryInterface(IAccessible), 146 indent + indent_step, indent_step) 115 147 except: 116 148 pass 117 149 elif child.vt == VT_DISPATCH: 118 inspect(child.value.QueryInterface(IAccessible), indent + step) 150 inspect(child.value.QueryInterface(IAccessible), 151 indent + indent_step, indent_step) 119 152 120 windll.user32.EnumWindows(prototype(callback), None) 153 if __name__ == '__main__': 154 # call our callback function on each window 155 windll.user32.EnumWindows(prototype(callback), None)
