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"