Auto-click using python. help needed!

Hi,

i would like some help writing a simple piece of code that will make the mouse left click automatically on 4 different positions on screen in a loop, looping as many times as necessary. I have python installed but code writing is not my strong suit.

Cheers

OS X Mavericks (10.9.1)

Posted on Jan 25, 2014 4:22 AM

Reply
1 reply

Jan 25, 2014 9:25 AM in response to ralber00

Here's a python script that clicks position 30,12


#!/usr/bin/python

from Quartz.CoreGraphics import * 
def mouseEvent(type, posx, posy):
        theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
        CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
        mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclick(posx,posy):
        mouseEvent(kCGEventLeftMouseDown, posx,posy);
        mouseEvent(kCGEventLeftMouseUp, posx,posy);
ourEvent = CGEventCreate(None); 
currentpos=CGEventGetLocation(ourEvent);    # Save current mouse position
mouseclick(30,12);
mousemove(int(currentpos.x),int(currentpos.y));  # Restore mouse position


see also: https://discussions.apple.com/thread/5811943?answerId=24613075022#24613075022


To loop, just use the for loop: https://wiki.python.org/moin/ForLoop


This loops 10 times around the screen:


#!/usr/bin/python

from Quartz.CoreGraphics import * 
def mouseEvent(type, posx, posy):
        theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
        CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
        mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclick(posx,posy):
        mouseEvent(kCGEventLeftMouseDown, posx,posy);
        mouseEvent(kCGEventLeftMouseUp, posx,posy);
ourEvent = CGEventCreate(None); 
currentpos=CGEventGetLocation(ourEvent);    # Save current mouse position
for x in range(0, 9):
          mouseclick(30,12);
          mouseclick(107,12);
          mouseclick(160,400);
          mouseclick(1200,400);
mousemove(int(currentpos.x),int(currentpos.y));  # Restore mouse position

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Auto-click using python. help needed!

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.