Two things:
-
Excel and Text Edit both have Applescript Dictionaries, so many things can be scripted directly rather than with UI scripting. What does your script actually do? Maybe you don’t need mouse clicks?
-
I modified the Python “drag” script I’d linked to before to simply click.
on mouseClick(x, y)
-- delayTime because the drag may fail if the UI isn't fast enough without a delay. For what I do, .1 works.
do shell script "
/usr/bin/python <<END
from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventCreate
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseUp
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap
import time
def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def mousedown(posxdown,posydown):
mouseEvent(kCGEventLeftMouseDown, posxdown,posydown);
def mouseup(posxup,posyup):
mouseEvent(kCGEventLeftMouseUp, posxup,posyup);
ourEvent = CGEventCreate(None);
mousedown(" & x & "," & y & ");
time.sleep(" & (".1") & ");
mouseup(" & x & "," & y & ");
END"
end mouseClick
So, you didn’t say when and where you need to click, but inserting some random clicks into your script it looks like this:
tell application "TextEdit" --{ ################### Text Editor | NotePad a #########}
activate
delay 9
end tell
tell application "System Events"
repeat 30 times
delay (random number from 0.2 to 0.9)
key code 125 -- {arrow down}
my mouseClick(1000, 50)
end repeat
end tell
tell application "System Events"
repeat 14 times
delay (random number from 0.2 to 0.9)
key code 126 -- {arrow up}
my mouseClick(20, 360)
end repeat
end tell
tell application "System Events"
repeat 8 times
delay (random number from 1.9 to 3.49)
key code 51 -- {delete}
my mouseClick(42, 42)
end repeat
end tell
tell application "System Events"
delay (random number from 10 to 60)
set textBuffer to " Hello "
repeat with i from 1 to count characters of textBuffer
keystroke (character i of textBuffer)
delay (random number from 1 to 5)
end repeat
end tell
tell application "System Events"
repeat 8 times
delay (random number from 1.9 to 3.49)
key code 51 -- {delete}
end repeat
end tell
mouseClick(1342, 91)
on mouseClick(x, y)
-- delayTime because the drag may fail if the UI isn't fast enough without a delay. For what I do, .1 works.
do shell script "
/usr/bin/python <<END
from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventCreate
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseUp
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap
import time
def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def mousedown(posxdown,posydown):
mouseEvent(kCGEventLeftMouseDown, posxdown,posydown);
def mouseup(posxup,posyup):
mouseEvent(kCGEventLeftMouseUp, posxup,posyup);
ourEvent = CGEventCreate(None);
mousedown(" & x & "," & y & ");
time.sleep(" & (".1") & ");
mouseup(" & x & "," & y & ");
END"
end mouseClick
Also, please use the Applescript tags when posting code. Button is just above text box when composing a comment.