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.

10 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

  2. was in exactly the same situation – did not achieve to an elegant solution – this is a very inventive approach.
    kudos!

  3. Would you have any idea why this isn’t working on my iMac running osx 10.8.4 ?
    My script is the same as yours with a little repeating:
    – – –
    repeat 50 times
    tell application “System Events” to key code 40
    end repeat
    – – –
    Keycode 40 is what moves the mouse down on my system.
    Running it does indeed type the letter k repeatedly in the applescript monitor, but it doesn’t move my mouse.

    What am I missing, please?
    Thanks,

    • Hi JC,
      I just tried this with my computer and as you described, key code 40 just typed a series of Ks.
      I suspect that, when mouse keys is enabled, it might actually be remapping the keyboard. I am guessing this because with an external keyboard (that has a numpad) plugged into my laptop k just types “k” and the numpad steers the mouse.

      Try firing off keycode 84 instead of 40 and let me know how that works for you.

  4. Hi
    I can’t seem to get this to work but would like to very very much.
    Do I need to save it as a script or an application? And do I put it in login items?
    Many thanks

    • So it looks like at some point an upgrade to this blog started replacing straight quotes with curly quotes, which caused apple script to error out on the above script. Updated this post so everything should work now.

  5. I found that it is ESSENTIAL to have a delay at the beginning of the script.

    For example,


    delay 3
    repeat 100 times
    tell application "System Events" to key code 88 --right
    end repeat

    For some reason, it won’t work without that delay

    • In my example, I had two dashes before it said “right”, to signify it as a comment. This website changed the double dashes to an en dash automatically.

Leave a Reply

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