Moving the mouse with applescript

While in Eugene this weekend setting up some art in a gallery installation, I found myself writing a piece of applescript to load a video, loop it, and place it in full screen whenever the computer booted up.

Then I encountered a small problem: when it loaded, the computer was placing the mouse cursor in the upper left corner of the screen, keeping the file menu visible, despite the full screen video.

Not wanting to make extra work for the gallery manager, I decided to solve the problem through applescript.

Now, there is not a direct command in applescript to control the mouse (which is a bummer, as that would have been incredibly easy.) but there is a way to emulate keystrokes. This is enough.

Universal Access allows you to use the number pad in place of a mouse

By enabling universal accessibility we can use key strokes to move the mouse.

To enable universal accessibility:

  • First, open up your system preferences, and go to “Universal Access.”
  • Next, go to “Mouse & Trackpad.”
  • Finally, turn on Mouse Keys.

Mouse keys allow you to use the number pad of the keyboard in place of a mouse.
Numpad 5 is click, and the numbers around it correspond to direction (2 down, 6 right, 3 down-right, and so on)

Once Mouse Keys are enabled, in applescript we can say
tell application “System Events” to key code ##
with ## being replaced with the corresponding integer value of the key code (which you can determine using Full Key Codes) and it will move the cursor roughly 1 pixel in that direction.

Repeat that line a bunch of times, and you can ensure that your cursor won’t be in the top corner of the screen.

2 thoughts on “Moving the mouse with applescript

  1. Pingback: When MouseKeys are on, how do I click or move the mouse using AppleScript? | MoVn - Linux Ubuntu Center

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>