OS X Scripting: How-to Script A Mouse Click At X-Y Coordinate

Automate annoying and repetitive mouse clicks with this great trick!

Requirements For This Walkthrough


  1. Internet Access (if downloading the file)
  2. Mac running OS X
  3. Xcode (if compiling on your own)


  1. Click click or MouseTools
  2. Xcode (if compiling on your own)

Knowledge, Skills, and Abilities

  • Ability to download a file, unzip it, and move it into a folder
  • Ability and confidence to enter commands in the Terminal, adjusting them to suit your environment, if necessary
  • Familiarity with common OS X file/directory locations
  • Understanding of basic computer terminoligy
  • Understanding of computer code is helpful, but not required


  • http://hints.macworld.com/article.php?story=2008051406323031

Installation Option One (Easier)

Download Click

[UPDATE]: It seems the original download link no longer works.  I have the file available to download from my Dropbox.  You may also be interested in MouseTools, which is a comparable program.

Download the pre-compiled binary from the Downloads section or visiting the link under Resources.

Unzip and Install Click

Unzip the file and move the click binary into /usr/bin .  This folder is hidden in the GUI by default, so use Terminal instead by entering the following command:

sudo mv ~/Downloads/click /usr/bin

Make the File Executable In Order to Run it

For good measure, add the executable bit to the click file

sudo chmod +x /usr/bin/click

Installation Option Two (Advanced)

Compile the Binary From the Source Code

Copy the following to a text file and save it as click.m

// File: 
// click.m
// Compile with: 
// gcc -o click click.m -framework ApplicationServices -framework Foundation
// Usage:
// ./click -x pixels -y pixels 
// At the given coordinates it will click and release.

#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>

int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];

// grabs command line arguments -x and -y
int x = [args integerForKey:@"x"];
int y = [args integerForKey:@"y"];

// The data structure CGPoint represents a point in a two-dimensional
// coordinate system. Here, X and Y distance from upper left, in pixels.
CGPoint pt;
pt.x = x;
pt.y = y;

// This is where the magic happens. See CGRemoteOperation.h for details.
// CGPostMouseEvent( CGPoint mouseCursorPosition,
// boolean_t updateMouseCursorPosition,
// CGButtonCount buttonCount,
// boolean_t mouseButtonDown, ... )
// So, we feed coordinates to CGPostMouseEvent, put the mouse there,
// then click and release.
CGPostMouseEvent( pt, 1, 1, 1 );
CGPostMouseEvent( pt, 1, 1, 0 );

[pool release];
return 0;

Compile it using gcc with the command:

gcc -o click click.m -framework ApplicationServices -framework Foundation

Install Click to /usr/bin

Move the click binary into /usr/bin .  This folder is hidden in the GUI by default, so use Terminal instead by entering the following command:

sudo mv click /usr/bin

Make the File Executable In Order to Run it

Add the executable bit to the click file

sudo chmod +x /usr/bin/click

Using Click

Whichever method you chose to install, using click is the same.  From Terminal, use the following syntax:

/usr/bin/click -x <coordinate> -y <coordinate>

Where <coordinate>  is equal to the x or y pixel-coordinate of the mouse.  But how do you find these coordinates?  Luckily, one of OS X’s built-in features can show you this.  Just press ⌘+⇧+4 (Command+Shift+4) and a little cross-hair with numbers will come up.  You can press Escape to get the cursor back.

Tweak to Make it Easier to Enter Commands

Edit your ~/.bash_profile  and add the following line

function cl() { /usr/bin/click -x "$1" -y "$2" ;}

Quit Terminal and re-open it.

Now, you should be able to type

cl <coordinate> <coordinate>

It it just a little more user-friendly because there is no need to type the -x and -y.  In addition, the cl , could be renamed to be anything you want.



One Reply to “OS X Scripting: How-to Script A Mouse Click At X-Y Coordinate”

Leave a Reply