Applescript Mouse Click

Hi Everyone,

I’m a total newbie when it comes to applescript so please forgive any ignorance. . .

I have the need for an applescript that I can run through automator along with some other functions that will allow me to open a particular web page, and click the mouse several times (in the same place).

I have found a neat little script that works a treat but I need to modify it to add a delay in the mouse click to allow the page to refresh/catch up.

Is there someone who can help me out?

Here’s the script I’m currently using:

set x to 1425
set y to 402
set l to 50 – click same location 50 times

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(kCGEventMouseMoved, 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, " & l & “):
mouseclick(” & x & “,” & y & “);
mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position
END”

Many thanks

Where is the AppleScript code :wink: More a question for a Python forum but here is your answer (see the line ‘time.sleep()’)


set x to 20
set y to 20
set l to 50 -- click same location 50 times

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(kCGEventMouseMoved, 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, " & l & "):
	mouseclick(" & x & "," & y & ");
	time.sleep(0.1); # delay
mousemove(int(currentpos.x),int(currentpos.y));      # Restore mouse position
END"

When you have AppleScript Toolbox installed, the code would look like:

set currentLocation to AST mouse point location
repeat 50 times
	AST click at {1425, 402}
	delay 0.1
end repeat
AST set mouse point location currentLocation

ha ha that’s how much of a newbie I am, I didn’t even realise this wash’t applescript :slight_smile:

Apple script toolbox looks simpler . . . I’ve downloaded the files but the instructions on your website on how to install this don’t correlate with my files?

I’m running 10.9.5

I’m actually using it inside of automator.

The earlier script I simply copied and pasted into the ‘run applescript’ automator as part of a workflow. And it works?

It also works if I paste it between the pre-defined applescript code:

on run {input, parameters}

(* Your script goes here *)

return input

end run

totally replacing the (* Your script goes here *) with the original code - is there a better/simpler way to do this?

many thanks for your time

You don’t have to use it if you’re fine with the current code you have now. But if you want to use it you can install it by unpacking the downloaded file and move the bundle into the folder “/Library/ScriptingAdditions”. If the folder ScriptingAdditions doesn’t exists in the Library folder of your startupdisk, you can create a new folder named “ScriptingAdditions” and move the file there. When once installed there, you may need to restart script editor/automator before it can be used (compiled).

Ok so I got working perfectly inside applescript editor but when I copy the same code into automator I get:

“Syntax Error
A identifier can’t go after this identifier.”

with AST Mouse highlighted.

How do I get the ASToolbox to work with automator??

It should be working right away. I’ve tested it both in Mavericks and Yosemite and both systems compiles and executes the scripts properly, I needed to restart Automator after installation. Does the Console show any warnings about Automator like:

Automator[pid]: Error loading /Library/ScriptingAdditions/AppleScript Toolbox.osax/Contents/MacOS/AppleScript Toolbox:  dlopen(/Library/ScriptingAdditions/AppleScript Toolbox.osax/Contents/MacOS/AppleScript Toolbox, 262): no suitable image found.

I wanted to reach out and thank DJ Bazzie Wazzie for pointing out ASToolbox. I was able to make good use of this product but ran into an issue. Many of my scripts perform workflow functions and run on headless Mac minis. Although the mouse click feature of ASToolbox works perfect on my main workstation, it does not on a headless Mac mini. Does anyone know if these ASToolbox features do not work on a headless computer.

My next step is to connect the Mac mini to a monitor and see if it works non-headless.

Which operating system is at work on your “headless” machines ?

If I remember well AppleScript Toolbox is supposed to run above 10.9 or higher operating sytems.

Yvan KOENIG (VALLAURIS, France) lundi 6 juillet 2015 15:38:28

To be honest I had no idea the command wouldn’t work when no screen is attached to the machine (While developing I have tested it with and without a mouse connected to the machine using remote apple events). Currently I have no machine where I can detach the screen from (iMacs and MacBook Pro’s only), so I’m in no position to test this right now unfortunately.

However, you could take a look in your console if there are any messages about this problem on the remote machine.

You can pm or mail me to look for an solution to make ASToolbox also perform well on headless machines.

@Yvan - Thanks I did update the computer to Yosemite before I started using the AppleScript Toolbox.

@DJ Bazzie Wazzie - I connected the Mac mini to a monitor and the AST mouse actions worked. The mouse actions continue to work now without a monitor. I am not sure what happened originally, but it works now.

Now I am again remembering all the annoyances of Yosemite.

Thanks again