Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Move and click the mouse via code' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Move and click the mouse via code
Authored by: TonyT on Jan 28, '11 09:50:56AM

Yes, I know, very old post, but here's a way to drag and drop with a python script:

#!/usr/bin/python
import sys
import time
from Quartz.CoreGraphics import * # imports all of the top-level symbols in the module

def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(posx,posy):
mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy):
mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(posx,posy):
mouseEvent(kCGEventLeftMouseDragged, posx,posy);

ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent); # Save current mouse position
mouseclickdn(60, 100);
mousedrag(60, 300);
mouseclickup(60, 300);
time.sleep(1);
mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position

Edited on Jan 28, '11 09:56:30AM by TonyT



[ Reply to This | # ]
Move and click the mouse via code
Authored by: willwade on Apr 07, '13 07:12:17AM
Thanks Tony - if anyone else wants anything similar I've expanded on this here (But also keep an eye on PyUserInput which hopes to do more..)

[ Reply to This | # ]