How Do I Get/Set Mouse Position?

I have done extensive searching, and I cannot find a way via native AppleScript to get/set mouse position. There are several shell scripts to do this (see MouseTools), but I’d prefer not to use them, unless there is no other choice.

I did find this scripting addition:
AppleScript Toolbox 2.0

Unfortunately, it has issues post El Capitan:


AppleScript Toolbox hasn’t had many updates lately. The latest version is stable and didn’t require any updates. However over time there are some functions that have an deprecated state in Sierra and should be updated properly.
. . .
Because Apple officially dropped support for Mountain Lion last year I expect that support for Mavericks will be dropped by the end of this year. Therefore I will release AST 3 within a few weeks where all commands who uses deprecated resources be updated.

AST 3 still has not been published.

So, is there an ASObjC method to get/set mouse position?

No. It’s done with C-based APIs that aren’t accessible to ASObjC.

That’s too bad.

Well, since my script is really for a Keyboard Maestro macro, I can just use this simple command:

tell application "Keyboard Maestro Engine" to set mousePosStr to process tokens "%CurrentMouse%"
set {mouseX, mouseY} to my splitStrToList(mousePosStr, ",")

EDIT: 2017-09-05 8:59 PM CT

Moved my remarks on KM vs Scripting to here:
Scripting vs Other Automation Tools (like Keyboard Maestro)

1 Like