Mouse click and move cursor in web browser

Hi there, I am trying to make a script where the mouse clicks 5-6 different points on the screen and repeats infinitely. It also needs to be able to start and stop/pause the script with a hot key (maybe shift+1).

You should take a look at mouseTools from Hank. You can move and click the mouse using this command line utility.

Hi, I’ve tried that but got nowhere with it. I don’t even know how to write a command line script like that? Any ideas?

Hello.

You should really find posts covering do shell script here. What you really need is executing commands sort of like this:

set mcmd to "/path/to/mouseClickCommand"
set xval to 22
set yval to 34
do shell script mcmd & " xpos = " & xval & " ypos= " & yval

And you have to make it work in a loop.

You’ll also need some utility to check for keypressed to get out of the loop:

repeat while true
	
	tell (do shell script " keypressed ")
		if result = "shft q" then exit repeat
	end tell
end repeat

All command names are fake. You may want to have a look at CommandKeys by StefanK for the keypressed, or it can be implemented more specifically with ASOC. You may also want to have a look at clickclick for mouseclicks. (I believe there are some examples there.

Happy Scripting. :slight_smile:

More specifically using Quartz, so you need an Objective-C wrapper class to use it in ASOC.

The example scripts of hank (at the bottom of the page) is very clear to me. The unix binary you will download you need to install somewhere and need to tell the script where you have installed it. I have installed it in a folder named bin of my home directory.


--Change this to the location where you have mouseTools installed.
set pathToMouseTools to "Macintosh HD:Users:DJ Bazzie Wazzie:bin:MouseTools" 

set xPos to 200
set yPos to 200

do shell script quoted form of POSIX path of pathToMouseTools & " -x " & (xPos as text) & " -y " & (ypos as text)

Hello.

I was more thinking of detecting whether a key be pressed or not, not if the key is pressed down or released, which I think is the difference in what you can do with NSEvent and the Quartz framework.

Shane has some examples that can be useful:

Edit
Quartz events doesn’t need a window, or a responder chain, which is the main difference, NSEvent may probably also use Quartz under the hood for all I know.

Ah thank you guys, ok so if I have 5 co-ordinates how would I write the script? Random numbers are fine.

set pathToMouseTools to “Macintosh HD:Users:DJ Bazzie Wazzie:bin:MouseTools”

set xPos to 200
set yPos to 200

then what does the rest of the code look like? Nothing happens when I try to add more positions below it.

Hello.

First of all, you’ll need ( I think) to set up the path to mouse tools as an alias, and then you’ll have to look at the repat loop above.

You’ll also need to figure out the random command, one easy way, is to have it return a value between zero and max, and then add your initial offset, so you’ll end up in the range you’ll have in mind.

set a to ((random number) * 300 as integer) + 200
-- should return values between 200 and 500

Thanks, I am unable to do it and my head hurts from trying! Also my mousetools is mousetools_unix with about 5 files in the folder so thats useless. Error: variable mouseToolsPath is not defined. Is it possible to write the whole code as I know nothing about scripting on mac?

I’m sorry, I made a typo in the first example

That’s the source code you’ve downloaded, you want/must download the compiled code (the link above) that says “the compiled command line tool”.

yes we can :slight_smile:

Now a dialog will appear for you to choose a file. Choose the file you have downloaded from hamsoft engineering (mouseTools; the compiled command line) and you’ll be fine. Then when you have done this, the second run it’ll know where the file is. Alias is just like a property persistent. Only when you recompile the code it will ask you again where mouseTools or when the alias is broken.


try
	pathToMouseTools
on error
	set pathToMouseTools to choose file
end try


set xPos to 200
set yPos to 200

do shell script quoted form of POSIX path of pathToMouseTools & " -x " & (xPos as text) & " -y " & (yPos as text)

I have made this so far, could someone please finish it for me? I just need it to repeat infinitely until a hotkey (shift+2) is pressed to end it, shift+1 would be used to start it as it needs to be run/manipulated from the background. Is it a better idea to write this in cocoa because of the limitations of applescript or is that going to be too hard?

 -- move the mouse to the x/y coordinates in 1000 steps
set mouseToolsPath to (path to home folder as text) & "UnixBins:MouseTools"

-- 1
set x to 1339.2
set y to 828

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & " -mouseSteps 1000" & " -leftClick"

-- 2
set x to 120.96
set y to 706.5

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & " -mouseSteps 1000" & " -leftClick"

-- 3
set x to 900
set y to 470.7

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & " -mouseSteps 1000" & " -leftClick"

-- 4
set x to 665.28
set y to 565.2

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & " -mouseSteps 1000" & " -leftClick"

-- 5
set x to 728.64
set y to 565.2

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & " -mouseSteps 1000" & " -leftClick"

-- 6
set x to 432
set y to 545.4

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & " -mouseSteps 1000" & " -leftClick"