Thursday, January 27, 2011

Simulating mouse click on Snow Leopard

After googling for 1 hour i came up with the following python script.

import sys
import time
from Quartz import *

def createMouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)

if __name__ == '__main__':
x = int(sys.argv[1])
y = int(sys.argv[2])

createMouseEvent(kCGEventLeftMouseDown, x, y);
createMouseEvent(kCGEventLeftMouseUp, x, y);

Usage: python 100 100
It'll move the mouse to (100, 100)

Here is the same script on github.

I used the following sources
metapep's blogpost
pepijndevos's PyMouse
GeekOrgy's Python script
a stackoverflow entry


No comments:

Post a Comment