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

Thanks for the heads up Mark. I was reading the documentation on those frameworks and noticed that CGWarpMouseCursorPosition() actually takes a CGPoint rather than an NSPoint, but NSPoint works because it’s a typealias of CGPoint. You could use application's CGPointMake(500, 500), but either way it’s just a struct containing 2 numeric (float) variables. I also noticed that calling application's is sufficient. So to make it simpler:

use framework "CoreGraphics"

on mouseTo(x, y)
	application's CGWarpMouseCursorPosition({x, y})
end mouseTo

mouseTo(500, 500)

This will move the mouse cursor and return 0. I left out the if theError = 1 then return "FAILURE" because it will never return 1. If there’s an error it will throw an error, it only returns 0 on success.

I didn’t think converting the 0 to "SUCCESS" was important because I usually see nothing returned on success in AppleScripts. The 0 only shows up in the Script Editor anyway. If it’s really annoying you could catch the return in an if statement:

use framework "CoreGraphics"

on mouseTo(x, y)
	if application's CGWarpMouseCursorPosition({x, y}) = 0 then
	end if
end mouseTo

mouseTo(500, 500)

If you were error catching and needed to identify success for additional code then checking for whether it = 0 is more convenient than = "SUCCESS", but I suppose a true would be even more convenient… So if you have that need:

use framework "CoreGraphics"

on mouseTo(x, y)
	try
		if application's CGWarpMouseCursorPosition({x, y}) = 0 then return true
	on error
		return false -- or whatever is desired
	end try
end mouseTo

if mouseTo(500, 500) then
	-- do stuff
else
	-- do other stuff
end if

You can alway change true to "SUCCESS" and false to "FAILURE" if desired; then change if mouseTo(500, 500) then to if mouseTo(500, 500) = "SUCCESS" then.