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
.