Login

Getting Mouse Coordinates

There was a time when AppleScripters had a cool tool named Extra Suites that, among other things, would get the current mouse coordinates. This osax stopped working with Leopard.

Here is a simple way to replace that functionality. Create a new Xcode project using the Foundation template. Replace the template code with the code below and compile. You now have a foundation tool that will retrieve those mouse coordinates!

As for moving the mouse and clicking, I have had great success with the command line utility "cliclick"
You can visit the webpage by clicking here.

Download CLI / Source

icon MouseLocation 10.4 (1.33 MB 2010-02-20 13:30:18)
icon MouseLocation 10.5 (1.85 MB 2010-02-20 13:30:46)

Leopard and above

Be sure to add the "Carbon" framework to the project
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>

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

    HIPoint point;
    HICoordinateSpace space = 2;
    HIGetMousePosition(space, NULL, &point);

	printf("%.2f %.2f", point.x, point.y);
	
    [pool drain];
    return 0;
}

Tiger

Be sure to add the "ApplicationServices" framework to the project
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>

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

    CGEventRef ourEvent = CGEventCreate(NULL);
    CGPoint point = CGEventGetLocation(ourEvent);
    CFRelease(ourEvent);
    printf("%.2f %.2f", point.x, point.y);
	
    [pool drain];
    return 0;
}

Add comment


Security code
Refresh

 

Product Categories