Setting a range of coordinates for an Auto Clicker

Hi, I was just wondering if there was a way to set a range of x,y coordinates for an Auto clicking program to randomly select. I am currently using Python wrapped in Applescript. Here's the code:


do shell script "

/usr/bin/python <<END

import sys

import time

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(" & x & "," & y & ");

mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position

END"

MacBook Pro

Posted on Sep 26, 2015 7:16 PM

Reply
6 replies

Sep 26, 2015 7:18 PM in response to YuuTang

Sorry code got cut off for some reason, here's the correct version:

do shell script "

/usr/bin/python <<END

import sys

import time

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(" & x & "," & y & ");

mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position

END"

Sep 27, 2015 3:03 PM in response to Tony T1

Hi, thanks for your response Tony! I tried defining my x and y coordinates to a random range using randrange(a,b) but its coming up as an undefined function. I put import random at the top, is there something else i need to put in? Sorry, I not very knowledgeable in coding, you may need walk me through somethings. Hopefully they aren't too long >.<

Sep 28, 2015 3:55 PM in response to YuuTang

Hi,


YuuTang wrote:


I tried defining my x and y coordinates to a random range using randrange(a,b) but its coming up as an undefined function.


The x and y variables are AppleScript's variables, they are green.

So you must use AppleScript command to set these variables, as Tony T1 told you:


set x to random number from 0 to 1800
set y to random number from 0 to 800
do shell script "/usr/bin/python <<END
import sys
import time
from Quartz.CoreGraphics import kCGEventMouseMoved, kCGHIDEventTap, CGEventCreateMouseEvent, CGEventCreate,  CGEventPost, CGEventGetLocation, kCGEventLeftMouseDown, kCGEventLeftMouseUp, kCGMouseButtonLeft
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(" & x & "," & y & ");
mousemove(int(currentpos.x),int(currentpos.y));      # Restore mouse position
END"



--------------


But, if you want a random from the python script, you can use randrange, like this

do shell script "/usr/bin/python <<END
from random import randrange
from Quartz.CoreGraphics import kCGEventMouseMoved, kCGHIDEventTap, CGEventCreateMouseEvent, CGEventCreate,  CGEventPost, CGEventGetLocation, kCGEventLeftMouseDown, kCGEventLeftMouseUp, kCGMouseButtonLeft
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
x, y = randrange(0, 1800), randrange(0, 800)
mouseclick(x, y);
mousemove(currentpos.x, currentpos.y);  # Restore mouse position
END"


The x and y variables are python's variables



Info: Import all from Quartz.CoreGraphics is very slow, it take 2.5 seconds to import *.

To import more quickly (less than 0.2 second):

you can import only what you need, as the proposed scripts, or use import Quartz.CoreGraphics as variable, like this script


do shell script "/usr/bin/python <<END
from random import randrange
import Quartz.CoreGraphics as qcg
def mouseEvent(type, posx, posy):
    theEvent = qcg.CGEventCreateMouseEvent(None, type, (posx,posy), qcg.kCGMouseButtonLeft)
    qcg.CGEventPost(qcg.kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
    mouseEvent(qcg.kCGEventMouseMoved, posx,posy);
def mouseclick(posx,posy):
    mouseEvent(qcg.kCGEventLeftMouseDown, posx,posy);
    mouseEvent(qcg.kCGEventLeftMouseUp, posx,posy);
ourEvent = qcg.CGEventCreate(None);
currentpos=qcg.CGEventGetLocation(ourEvent); # Save current mouse position
x, y = randrange(0, 1800), randrange(0, 800)
mouseclick(x, y);
mousemove(currentpos.x, currentpos.y);  # Restore mouse position
END"

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.

Setting a range of coordinates for an Auto Clicker

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