Control the mouse on Mac with Python!

It’s 00:30 now here… After searching the internet for the whole day I finally found to move AND click the mouse with Mac OS X!!!

This link on StackOverflow got me on the right path. It explains how to use pyobjc to move the mouse around, but this does not include clicking it 😦 After a few more hours of Googling I found this extra piece of information for writing a command line click utility. But the pyobjc nor the Apple functions are documented very well, so I had absolutely no clue on how get the function into Python.

Then I finally found this xml document… I had nearly did away with it as it looked useless at first. Until I noticed that at the function described in the first document it showed {CGPoint=ff}, this piece of code was confusing me the most about this whole stuff. Well, the rest was guesswork at the interactive shell… ‘v{CGPoint=ff}III’ is the final string I came up with.

So here they are!

import objc

def clickMouse(x, y, button):
    bndl = objc.loadBundle('CoreGraphics', globals(), '/System/Library/Frameworks/ApplicationServices.framework')
    objc.loadBundleFunctions(bndl, globals(), [('CGPostMouseEvent', 'v{CGPoint=ff}III')])
    CGPostMouseEvent((x, y), 1, button, 1)
    CGPostMouseEvent((x, y), 1, button, 0)

def moveMouse(x, y):
    bndl = objc.loadBundle('CoreGraphics', globals(), '/System/Library/Frameworks/ApplicationServices.framework')
    objc.loadBundleFunctions(bndl, globals(), [('CGWarpMouseCursorPosition', 'v{CGPoint=ff}')])
    CGWarpMouseCursorPosition((x, y))

How I think it works:

  1. The first line works like an import statement to include CoreGraphics… nothing Python related happens here.
  2. The second line is the most important one. From the just imported bundle it imports the C function to Python… I think the second part is for mapping Python data types to C data types.
  3. Line 3 and 4 run the fresh C function with Python data types! (once for keyDown and once for keyUp, otherwise Finder gets stuck)



7 Responses to “Control the mouse on Mac with Python!”

  1. 1 Mack
    August 17, 2009 at 9:22 am

    This was absolutely what I was looking for. And like you, I spent much of the day looking for answers. I had seen the CGWarpMouseCursorPosition function at the first link you posted but didn’t see the xml document.

    In the loadBundleFunctions I’m not sure totally what the syntax is for ‘v{CGPoint=ff}III’. I don’t know what the ‘v’ is for, but the {CGPoint=ff} seems to define the argument as (float,float), while in your case III indicates three integer values. This seems to register to function in Python as having that argument list.

    After looking at CGPostMouseEvent in CGReemoteOperation.h I think the syntax has a bit different meaning:

    CGPostMouseEvent( (x,y), bool to move mouse to event location, the button count, then a total number of booleans represented by button count stating if buttons are down. It explains that it takes the state you gave it, and compares it to the last state information it has to generate the proper event type.

    example. I want to generate and hold a right-click on a 3-button mouse:
    CGPostMouseEvent( (100,100), true, 3, false, false, true)

    now I want to stop the hold
    CGPostMouseEvent( (100,100), true, 3, false, false, false)

  2. 2 pepijndevos
    August 22, 2009 at 7:34 pm

    I don’t know where the v is for, but it was in an example and it worked so I leaved it in… I’ve got no clue about the syntax, except for the fact that it’s an argument list.

    Thanks for your information!

  3. 3 Wido
    September 21, 2009 at 8:24 am

    Hi Pepijn. I’m writting you through this site. I just found your pymouse library and i think is really cool. I’m about to start a project with a friend and this module is probably going to be one of the cores (will have a couple more of stuff πŸ˜‰ )
    please mail me back as I wasn’t able to find you mail (and i think you’re able to read mine in this comment, right?) as i will probably touch something and would like to commit that to your project


  4. 4 pepijndevos
    September 21, 2009 at 8:37 am

    I’ll send you an email… I’m looking forward to your changes!

  5. March 14, 2010 at 3:37 pm

    i have been on the same track as you here, i got it ‘moving’ but i could not get it clicking.
    thankyou so much for your code its perfect! πŸ™‚

    i am writing a touchscreen driver which uses a chinese raw data (serial) touchscreen, and i use python (in a screen session) to parse the data (ms HID format) and then move and click the mouse accordingly.

    its perfecto! πŸ™‚ thanks!

  6. 6 pepijndevos
    March 14, 2010 at 6:56 pm

    Have you seen the full PyMouse lib? I developed this further and made it into a github project: http://github.com/pepijndevos/PyMouse

Comments are currently closed.

My blog has moved!

My blog has permanently moved to a self hosted Wordpress at http://pepijndevos.nl

This blog will stay around for accidental search engine visitors.


This is me

Blog Stats

  • 22,233 hits


%d bloggers like this: