Move the cursor to a certain coordinate on the screen.

To click on a screen coordinate, without modifying the cursor position, the following script would be sufficient:

tell application “System Events”
click at {1565, 52}
end tell

However, what code would place the cursor at a certain coordinate on the screen?

Thanks in advance.

You could possibly do this by creating an NSEvent
And then sending it:

https://developer.apple.com/documentation/appkit/nsapplication/1428359-sendevent?language=objc

I’m not sure if you create a mouse event or custom event.
There is the CursorMoved or CursorUpdate types.
Maybe a MouseMoved with out a click.

https://developer.apple.com/documentation/appkit/nsevent/1532495-mouseeventwithtype?language=objc

EventTypes:

https://developer.apple.com/documentation/appkit/nseventtype?language=objc

Why is it that you don’t want to click?

The question asked by the OP is not interesting. Since it has been asked on the forums many times and exhaustive answers have already been given to it. I am aware of at least 3 methods for moving the mouse pointer on the screen.

  1. using a third-party utility like ClicClick, 2) using Python, 3) using CGPostMouseEvent. Moreover, the latter method is not recommended and has been deprecated, although it still works …

All examples of these methods are already published on this site and the OP can find them himself.

It would be much more interesting if someone shared a concrete example of the new method you point to here. I have not seen such an example yet, so I have a hunch that it is difficult or impossible to apply it with the AsObjC language.

This is a total hack, but does work, although it should not work.
So use with caution.


use framework "Foundation"
use framework "CoreGraphics"

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

set theError to current application's CGDisplayMoveCursorToPoint(current application's CGMainDisplayID(), cursorPoint)

The “theError” variable should hold a CGError if it fails, but I thought that AS can’t handle CGError’s.
Also the “CGMainDisplayID” should be a “UInt32” data type, but you can pass any integer value in the above code, which again should not be working.

So I’m not sure why it’s working, but it does on “Mojave” at least, so again use with caution.

Regards Mark

Works great with Monterey.

@peavine

I’m guessing that the “theError” is probably returning “0” for success, and “1” if a failure occurs.
So an if statement might be a good idea, like this.


use framework "Foundation"
use framework "CoreGraphics"

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

set theError to current application's CGDisplayMoveCursorToPoint(current application's CGMainDisplayID(), cursorPoint)

if theError = 0 then
	return "SUCCESS"
else if theError = 1 then
	return "FAILURE"
end if

But just a guess, maybe Shane knows how to handle CGError’s in AppleScript.

This is a better solution, because it does not rely on a display ID number, but automatically uses the default or main screen.


use framework "Foundation"
use framework "CoreGraphics"

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

set theError to current application's CGWarpMouseCursorPosition(cursorPoint)

if theError = 0 then
	return "SUCCESS"
else if theError = 1 then
	return "FAILURE"
end if

Regards Mark