Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Screen-scraping and controlling mouse in OS X

I'm looking into screen-scraping and controlling the mouse in OS X for a hobby project.

I'm not looking for the most elegant way, but I need to be able to capture the screen every half a second or so.

I've found that I could use the screencapture command-line tool (screencapture -w -W -i ~/Desktop/capture.jpg), but I'm worried that it might not be fast enough.

I'm also looking for a way to send clicks, set the cursor position and obtain the cursor position. Sort of like what win32api provides: mouse_event, SetCursorPos and GetCursorPos.

I've found this sample code that uses the PyObjC library to set the cursor position, but it's always moving my mouse to (0,0) instead of the coordinates I pass it.

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(500, 500)

Edit: I'm running Snow Leopard (10.6) if it matters.

Thanks!

like image 459
Christian Joudrey Avatar asked Dec 23 '10 04:12

Christian Joudrey


2 Answers

Take a look at autopy. Works in OSX, Linux and Windows.

like image 77
Rumple Stiltskin Avatar answered Sep 23 '22 13:09

Rumple Stiltskin


If you're just screenscraping, might I suggest you try Sikuli? It makes interface automation like child's play, and the scripting uses Python.

like image 36
jathanism Avatar answered Sep 22 '22 13:09

jathanism