Skip to content

Instantly share code, notes, and snippets.

@unix-beard
Created June 28, 2015 12:12
Show Gist options
  • Save unix-beard/b311d8620429ce4ed918 to your computer and use it in GitHub Desktop.
Save unix-beard/b311d8620429ce4ed918 to your computer and use it in GitHub Desktop.
This is how you control the mouse programmatically under Mac OS X
import time, sys
from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventCreate
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import CGEventGetLocation
from Quartz.CoreGraphics import kCGEventMouseMoved
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseUp
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap
def m_event(type_, x, y):
e = CGEventCreateMouseEvent(None, type_, (x, y), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, e)
def m_move(x, y):
m_event(kCGEventMouseMoved, x, y)
def m_click(x, y):
m_event(kCGEventLeftMouseDown, x, y)
m_event(kCGEventLeftMouseUp, x, y)
x, y = int(sys.argv[1]), int(sys.argv[2])
e = CGEventCreate(None); cur_pos=CGEventGetLocation(e)
m_click(x, y);
time.sleep(0.2);
m_click(x, y);
m_move(cur_pos.x, cur_pos.y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment