Move mouse/cursor

Is it possible, with AppleScriptObjC, to move the mouse/cursor to a given position?
Thanks to Shane, I’m able to get the mouse position with: (current application's NSEvent's mouseLocation()) as list but can’t figure out how to set it.

Is there a Core Graphics method usable with Applescript or any other hidden gem?

Jonas. I use the solution posted by Mark_FX on the MacScripter’s site:

use framework "Foundation"
use framework "CoreGraphics"

set cursorPoint to current application's NSMakePoint(500, 500)
current application's CGWarpMouseCursorPosition(cursorPoint)

The thread can be found here

1 Like

Here is other from Macscripter.net, it use PyObjC, I test it and its work. I click the Apple logo in top left corner.

set {x, y} to {10, 10}

do shell script "

/usr/bin/python3 <<EOF
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(kCGEventLeftMouseDown, posx,posy);
          mouseEvent(kCGEventLeftMouseUp, posx,posy);
		  
ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent);             # Save current mouse position
mouseclick(" & x & "," & y & ");
mousemove(int(currentpos.x),int(currentpos.y));      # Restore mouse position
EOF"

There’s also a very useful tool called cliclick (i.e. CLI click) that you can call with do shell script. It’s available on Homebrew.

1 Like

Thanks guys.
I dont’t want to simulate a click. That’s why I will use @peavine suggestion.

Here’s one more option:

set displayID to current application's CGMainDisplayID()
set cursorPoint to current application's NSMakePoint(10, 10)
set hadError to current application's CGDisplayMoveCursorToPoint(displayID, cursorPoint)
if hadError as boolean then error "Set mouse position failed."
1 Like

Oops. Forgot the first, rather important, line of the code:

use framework "Foundation"