Saturday, November 27, 2021

#1 2021-11-17 03:47:23 am

DJUNQUERA
Member
Registered: 2011-04-22
Posts: 32

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.

Offline

 

#2 2021-11-25 01:54:08 am

technomorph
Member
Registered: 2017-12-14
Posts: 218

Re: Move the cursor to a certain coordinate on the screen.

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

https://developer.apple.com/documentati … guage=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/documentati … guage=objc

EventTypes:

https://developer.apple.com/documentati … guage=objc

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

Online

 

#3 2021-11-25 05:00:27 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: Move the cursor to a certain coordinate on the screen.

technomorph wrote:

You could possibly do this by creating an NSEvent


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.


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Online

 

#4 2021-11-25 02:11:03 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 152

Re: Move the cursor to a certain coordinate on the screen.

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

Applescript:


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

Last edited by Mark FX (2021-11-25 03:28:03 pm)

Offline

 

#5 2021-11-25 02:29:32 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Move the cursor to a certain coordinate on the screen.

Mark FX wrote:

So I have no idea why it's working, but it does on "Mojave" at least, so again use with caution.



Works great with Monterey.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#6 2021-11-25 02:42:15 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 152

Re: Move the cursor to a certain coordinate on the screen.

@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.

Applescript:


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.

Offline

 

#7 2021-11-25 02:52:31 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 152

Re: Move the cursor to a certain coordinate on the screen.

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

Applescript:


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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)