Alex Rivera | Logout

How to control the mouse in Mac using Python?

Asked 2008-11-11T15:04:43.577
42

What would be the easiest way to move the mouse around (and possibly click) using Python on OS X?

This is just for rapid prototyping, it doesn't have to be elegant.

Edit
Report

1 Answer

12

Just try this code:

#!/usr/bin/python

import objc

class ETMouse():    
    def setMousePosition(self, x, y):
        bndl = objc.loadBundle('CoreGraphics', globals(), 
                '/System/Library/Frameworks/ApplicationServices.framework')
        objc.loadBundleFunctions(bndl, globals(), 
                [('CGWarpMouseCursorPosition', 'v{CGPoint=ff}')])
        CGWarpMouseCursorPosition((x, y))

if __name__ == "__main__":
    et = ETMouse()
    et.setMousePosition(200, 200)

it works in OSX leopard 10.5.6

answered 2009-03-19T23:15:07.620

Your Answer