Skip to content

Instantly share code, notes, and snippets.

@cowboytronic
Created July 23, 2008 08:30
Show Gist options
  • Save cowboytronic/1613 to your computer and use it in GitHub Desktop.
Save cowboytronic/1613 to your computer and use it in GitHub Desktop.
// File:
// clicks.m
//
// Compile with:
// gcc -o clicks clicks.m -framework ApplicationServices -framework Foundation
//
// Usage:
// ./clicks -x 10 -y 10 (or whatever coordinates you want)
// it will click and release, but you can edit the code for the desired effect
//
// Yes, it is an ugly mix of Cocoa and Carbon.
// Yes, it could be rewritten in only a few lines of code.
// Whatever.
#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
float x = [args floatForKey:@"x"];
float y = [args floatForKey:@"y"];
// Turns x and y values into real coordinates. The data structure CGPoint
// represents a point in a two-dimensional coordinate system.
CGPoint pt;
pt.x = x;
pt.y = y;
// Uncomment the line below to have coordinates spit out to command line.
// NSLog(@"x = %f, y = %f", pt.x, pt.y);
// This is where the magic happens. See CGRemoteOperation.h for details.
//
// CGPostMouseEvent( CGPoint mouseCursorPosition,
// boolean_t updateMouseCursorPosition,
// CGButtonCount buttonCount,
// boolean_t mouseButtonDown, ... )
//
// So here I'm feeding it coordinates, telling it to YES update the mouse
// position, use mouse button 1, and click then release.
CGPostMouseEvent( pt, 1, 1, 1);
CGPostMouseEvent( pt, 1, 1, 0);
[pool release];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment