forum.latenightsw.com Open in urlscan Pro
104.131.190.190  Public Scan

URL: https://forum.latenightsw.com/t/move-mouse-cursor/4247
Submission: On July 02 via manual from FR — Scanned from FR

Form analysis 1 forms found in the DOM

POST /login

<form id="hidden-login-form" method="post" action="/login" style="display: none;">
  <input name="username" type="text" id="signin_username">
  <input name="password" type="password" id="signin_password">
  <input name="redirect" type="hidden">
  <input type="submit" id="signin-button" value="Log In">
</form>

Text Content

Late Night Software Ltd.


MOVE MOUSE/CURSOR

AppleScript
asobjc

ionah (Jonas Whale) March 12, 2023, 4:05pm #1

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?




peavine (peavine) March 12, 2023, 8:22pm #2

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


Fredrik71 (Fredrik Gustafsson) March 12, 2023, 8:24pm #3

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"





tree_frog March 13, 2023, 1:39pm #4

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


ionah (Jonas Whale) March 13, 2023, 2:16pm #5

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




StanC (Stan Cleveland) March 13, 2023, 3:09pm #6

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


StanC (Stan Cleveland) March 13, 2023, 7:25pm #7

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

use framework "Foundation"



 * Home
 * Categories
 * FAQ/Guidelines
 * Terms of Service
 * Privacy Policy

Powered by Discourse, best viewed with JavaScript enabled


LATE NIGHT SOFTWARE

 * Script Debugger 8
 * Download
 * Buy
 * Forum
 * Support
 * Blog
 * Company

Skip to main content
Sign UpLog In
 * 
 * 


MOVE MOUSE/CURSOR

AppleScript
asobjc

You have selected 0 posts.

select all

cancel selecting

Mar 2023
1 / 7
Mar 2023

Mar 2023

ionahJonas Whale
Mar '23


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?

Solved by peavine in post #2


> 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





 * CREATED
   
   Mar '23

 * LAST REPLY
   
   Mar '23
 * 6
   
   REPLIES

 * 3.2k
   
   VIEWS

 * 5
   
   USERS

 * 3
   
   LIKES

 * 3
   
   LINKS

 * 2
   2
   

peavine
Mar '23


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 73

Solution
1


Fredrik71Fredrik Gustafsson
Mar '23


Here is other from Macscripter.net 25, 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"





tree_frog
Mar '23


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

1


ionahJonas Whale
Mar '23


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




StanCStan Cleveland
Mar '23


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


StanCStan Cleveland
Mar '23


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

use framework "Foundation"







Reply


SUGGESTED TOPICS

Topic Replies Views Activity Illustrator script failing to select characters
AppleScript
sd8
2 127 7d

Weird failure of the Finder’s duplicate command
AppleScript
7 111 4d Migration to new Mac M3 fudged FMDBAS
AppleScript
sqlite-lib2
3 96 3d Intermittent SMSTableDialogController (null selector) error on Myriad
Tables
AppleScript
myriad-tables
3 127 11d Mail - delete mailbox
AppleScript
3 236 15d


WANT TO READ MORE? BROWSE OTHER TOPICS IN APPLESCRIPT OR VIEW LATEST TOPICS.

Share








Invalid date Invalid date


Mastodon