|
// File: |
|
// clickdrag.m |
|
// |
|
// clickdrag will drag the "mouse" from the current cursor position to |
|
// the given coordinates. |
|
// |
|
// derived from: |
|
// http://www.macosxhints.com/article.php?story=2008051406323031 |
|
// & |
|
// http://davehope.co.uk/Blog/osx-click-the-mouse-via-code/ |
|
// |
|
// Requires: |
|
// xtools |
|
// |
|
// Compile with: |
|
// gcc -o clickdrag clickdrag.m -framework ApplicationServices -framework Foundation |
|
// |
|
// Usage: |
|
// ./clickdrag -x pixels -y pixels |
|
// |
|
|
|
|
|
|
|
#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"]; |
|
|
|
int dx = [args integerForKey:@"dx"]; |
|
int dy = [args integerForKey:@"dy"]; |
|
|
|
|
|
// The data structure CGPoint represents a point in a two-dimensional |
|
// coordinate system. Here, X and Y distance from upper left, in pixels. |
|
// |
|
CGPoint pt0; |
|
pt0.x = x; |
|
pt0.y = y; |
|
|
|
CGPoint pt1; |
|
pt1.x = x + dx; |
|
pt1.y = y + dy; |
|
|
|
CGPoint pt2; |
|
pt2.x = x + dx / 2; |
|
pt2.y = y + dy / 2; |
|
|
|
// This is where the magic happens. See CGRemoteOperation.h for details. |
|
|
|
// Get the current mouse location |
|
// CGEventRef ourEvent = CGEventCreate(NULL); |
|
// CGPoint ourLoc = CGEventGetLocation(ourEvent); |
|
|
|
// CGPostMouseEvent( CGPoint mouseCursorPosition, |
|
// boolean_t updateMouseCursorPosition, |
|
// CGButtonCount buttonCount, |
|
// boolean_t mouseButtonDown, ... ) |
|
// |
|
|
|
// Start drag from current cursor location |
|
CGPostMouseEvent( pt0, TRUE, 1, FALSE ); |
|
CGPostMouseEvent( pt0, FALSE, 1, TRUE ); |
|
|
|
// End drag by moving to new location |
|
CGPostMouseEvent( pt1, TRUE, 1, TRUE ); |
|
CGPostMouseEvent( pt1, FALSE, 1, FALSE ); |
|
|
|
sleep(1); |
|
|
|
// Click middle |
|
CGPostMouseEvent( pt2, TRUE, 1, TRUE ); |
|
CGPostMouseEvent( pt2, FALSE, 1, FALSE ); |
|
|
|
// Possible sleep routines |
|
//usleep(100000); |
|
//sleep(2); |
|
|
|
[pool release]; |
|
return 0; |
|
} |