Christian Joudrey
Christian Joudrey

Reputation: 3461

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!

Upvotes: 5

Views: 4884

Answers (3)

Rumple Stiltskin
Rumple Stiltskin

Reputation: 10425

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

Upvotes: 4

hoju
hoju

Reputation: 29472

Would automation software like ControllerMate help?

allows you to customize the behavior of your HID devices — keyboards, keypads, mice, trackballs, joysticks, gamepads, throttles, among others

Upvotes: 0

jathanism
jathanism

Reputation: 33726

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

Upvotes: 2

Related Questions