-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseLocation.m
More file actions
61 lines (51 loc) · 1.74 KB
/
MouseLocation.m
File metadata and controls
61 lines (51 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
#import <AppKit/AppKit.h>
#define defs [NSUserDefaults standardUserDefaults]
void WriteStringToPasteboard(NSString *string) {
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pb setString:string forType:NSStringPboardType];
[pb stringForType:NSStringPboardType];
}
void ShowHelp() {
printf("Usage:\n");
printf(" MouseLocation [h|help|fp]\n");
printf(" -h or -help will display this help message\n");
printf(" -p will copy the mouse coordinates to pasteboard\n");
printf(" -f will format the output for use with the cliclick cli.\n");
printf("\n");
printf("You cannot put 'f' and 'p' together like -fp.\n");
printf("They must be separate and each one preceeded by a dash like '-f -p'.\n");
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *mouseCoordinates;
NSArray *help = [NSArray arrayWithObjects:@"-h", @"-help", nil];
NSArray *pInfo = [NSArray arrayWithArray:[[NSProcessInfo processInfo] arguments]];
if ( [pInfo count] == 1 ) {
ShowHelp();
}
else if ( [help containsObject:[pInfo objectAtIndex:1]] ) {
ShowHelp();
}
else {
HIPoint point;
HICoordinateSpace space = 2;
HIGetMousePosition(space, NULL, &point);
if ( [pInfo containsObject:@"-f"] ) {
mouseCoordinates = [NSString stringWithFormat:@"cliclick %.2f %.2f", point.x, point.y];
}
else {
mouseCoordinates = [NSString stringWithFormat:@"%.2f %.2f", point.x, point.y];
}
if ( [pInfo containsObject:@"-p"] ) {
WriteStringToPasteboard(mouseCoordinates);
}
else {
printf("%s", [mouseCoordinates UTF8String]);
}
}
[pool drain];
return 0;
}