So I've been trying to make a program using AppleScript, but there's one thing that's been teasing me for some time now - How do I make the mouse click at current location without using Mouse Keys?
Also, is it possible to simulate a long click (that means I set the length of it)?
Asked
Active
Viewed 4.1k times
9

Prokop Hanzl
- 1,165
-
See https://stackoverflow.com/questions/38766699/applescript-click-at-mouse-position – rogerdpack Oct 05 '18 at 21:18
-
It's always better, if you can, to do it programmatically, rather than replicate 'human' activity -- e.g. use commands to set GUI control values, press OK, etc. – benwiggy Nov 06 '22 at 21:27
2 Answers
15
You can simulate a mouse click with AppleScript code like this:
tell application "System Events"
click at {123,456}
end tell
This means to simulate a click of the mouse at coordinate (123,456).
To click at the current location you'll need to first find the current location of the mouse pointer, if you don't know it already. This is not easily done with AppleScript itself, so a neat way is to use the third party tool "MouseTools".
You can download it for free here.
On the download webpage you'll find an AppleScript example for getting the current mouse coordinates.

jksoegaard
- 77,783
-
-
also, you said it isn't easily done, does that mean it is possible w/out MouseTools? it'd be awesome if it worked on other computers which don't have MT downloaded, else I'd have to bundle it w/ my program – Prokop Hanzl Dec 30 '16 at 12:19
-
and one more thing - you seem smart about AppleScript - would you please check out my unanswered questions? I have a feeling you might know the answers - thanks – Prokop Hanzl Dec 30 '16 at 12:22
-
@ProkopHanzl It is both easily done and possible yes. I said it was not easy WITHOUT MouseTools (or similar), but with MouseTools it is very easy. Without MouseTools you would have to find your own solution. – jksoegaard Dec 30 '16 at 13:07
-
@ProkopHanzl Regarding your other, non-related questions - I have no real experience with AppleScript, so you'll have to look elsewhere for answers. – jksoegaard Dec 30 '16 at 13:08
-
-
4To supplement @jksoegaard's answer: one can also find cursor positions using "Screenshot" tool, which may be activated by
Cmd+Shift+4
shortcut. Then just point the cursor to any area and remember the numbers that is shown near the pointer. – Andrey Semakin Jan 26 '20 at 09:10 -
This answer deserves kudos solely for the fact that it got me pointed towards "MouseTools" which was everything I needed and more. For those who install it remember to either put it somewhere where $path is already looking or update /etc/paths. – Art Geigel Jul 27 '21 at 03:48
-
A few days ago around 20-OCT-2021 the hamsoftengineering.com domain has expired and no longer hosting the site, is there a mirror or alternate download location? – Jay Taylor Oct 29 '21 at 17:51
-
I found that too, and found an alternative called cliclick -- https://github.com/BlueM/cliclick. It's super easy to use from Terminal, but I have no idea how to use it in Automator. Seems possible though: https://apple.stackexchange.com/questions/276047/how-do-the-cliclick-and-mousetools-command-line-tools-differ – supergra Nov 04 '21 at 21:53
-
1
Long click is much easier with Hammerspoon, place something like this inside init.lua
and reload:
hs.hotkey.bind({"alt"}, "L", function()
hs.eventtap.leftClick(hs.mouse.getAbsolutePosition(), 500000)
end)
This will (on typing keyboard shortcut Alt+L): left click on the current mouse cursor position, with half a second delay between click down and up.

Kamal
- 250