On Nov 19, 2007 5:54 AM, Thomas Heller <[EMAIL PROTECTED]> wrote: > Shane Clark schrieb: > > Hi, > > > > I am trying to get my python app to output the name of the control > > under the mouse each time it is clicked. Currently, I am trying to do > > this with a combination of pyhook and pyAA, but pyAA gives me errors > > for almost any control in Microsoft Office and some other apps that I > > need to support. > > > > I have seen comtypes referenced several times while searching for > > answers, but I am intermediate at best in Python and do not have great > > knowledge of COM in general. Could anyone point me in the right > > direction on how I might get this functionality using comtypes? > > I have no idea what the problems with pyhook or pyAA are, but I made a simple > script for comtypes that works for me, with comtypes 0.4.0. When I have MS > Word > open, and the cursor is over the 'Save' button, the document is saved when I > run the script. > It may get you started. > > Thomas > > <code> > from ctypes import oledll, windll, byref, POINTER > from ctypes.wintypes import POINT > from comtypes.client import wrap > from comtypes.automation import VARIANT > from comtypes import IUnknown > > oleacc = oledll.oleacc > > def AccessibleObjectFromPoint(x, y): > pacc = POINTER(IUnknown)() > var = VARIANT() > oleacc.AccessibleObjectFromPoint(POINT(x, y), > byref(pacc), > byref(var)) > return wrap(pacc), var.value > > def GetCursorPos(): > pt = POINT() > windll.user32.GetCursorPos(byref(pt)) > return pt.x, pt.y > > if __name__ == "__main__": > x, y = GetCursorPos() > pacc, value = AccessibleObjectFromPoint(x, y) > print pacc.accName() > print pacc.accDefaultAction() > print pacc.accDoDefaultAction() > <code/> > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2005. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > comtypes-users mailing list > comtypes-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/comtypes-users >
I incorporated that snippet into a little test script and I still have trouble with Office controls. If I run your example all is well, but mine gives the following error: "WindowsError: [Error -2147417843] An outgoing call cannot be made since the application is dispatching an input-synchronous call" Presumably, this is a timing problem, as I found references to it in other contexts. I have no idea how to resolve it at this point, but I am exploring it. Has anyone else seen this error from comtypes? My code is below. <code> import pythoncom, pyHook from ctypes import oledll, windll, byref, POINTER from ctypes.wintypes import POINT from comtypes.client import wrap from comtypes.automation import VARIANT from comtypes import IUnknown oleacc = oledll.oleacc def AccessibleObjectFromPoint(x, y): pacc = POINTER(IUnknown)() var = VARIANT() oleacc.AccessibleObjectFromPoint(POINT(x, y), byref(pacc), byref(var)) return wrap(pacc), var.value def GetCursorPos(): pt = POINT() windll.user32.GetCursorPos(byref(pt)) return pt.x, pt.y def OnMouseClick(event): x,y = GetCursorPos() pacc, value = AccessibleObjectFromPoint(x, y) print pacc.accName() # return True to pass the event to other handlers return True if __name__ == "__main__": # create a hook manager hm = pyHook.HookManager() # watch for left-click events hm.MouseLeftDown = OnMouseClick # set the hook hm.HookMouse() # wait forever pythoncom.PumpMessages() </code> Thanks, -Shane Clark ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ comtypes-users mailing list comtypes-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/comtypes-users