Advertisement
Guest User

Untitled

a guest
Aug 8th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  AppDelegate.m
  3. //  MouseTracker
  4. //
  5.  
  6. #import <Cocoa/Cocoa.h>
  7. #import <AppKit/NSAccessibility.h>
  8.  
  9. #import "AppDelegate.h"
  10. #import "UIElementUtilities.h"
  11.  
  12. // these are private
  13. @interface AppDelegate () {
  14.  
  15. }
  16.  
  17. @property (weak) IBOutlet NSWindow *window;
  18. @end
  19.  
  20. @implementation AppDelegate
  21.  
  22. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  23. {
  24.     if (AXIsProcessTrustedWithOptions(NULL)) {
  25.     //    NSLog(@"Trusted I guess");
  26.     }
  27.     if (!AXAPIEnabled()) {
  28.         NSLog(@"Accessibility API is not enabled, go to System Preferences");
  29.     }
  30.     NSLog(@"Creating system wide accessibility object");
  31.     _systemWideAccessibilityObject = AXUIElementCreateSystemWide();
  32.     NSLog(@"%@", CFCopyDescription(_systemWideAccessibilityObject));
  33.  
  34.     _currentUIElement = NULL;
  35.  
  36.     // if (timer == nil) ...
  37.     timer = [NSTimer scheduledTimerWithTimeInterval:0.5
  38.                                              target:self
  39.                                            selector:@selector(checkMouse:)
  40.                                            userInfo:nil
  41.                                             repeats:YES];
  42. }
  43.  
  44. - (void)dealloc {
  45.     [timer invalidate];
  46.     timer = nil;
  47.    
  48.     if (_systemWideAccessibilityObject)
  49.         CFRelease(_systemWideAccessibilityObject);
  50.     if (_currentUIElement)
  51.         CFRelease (_currentUIElement);
  52.     [super dealloc];
  53. }
  54.  
  55. - (void)checkMouse:(NSTimer *)theTimer
  56. {
  57.     mouseLocation = [NSEvent mouseLocation];
  58.    
  59.     // check to see if the mouse even moved
  60.     if (!NSEqualPoints(mouseLocation, lastMouseLocation)) {
  61.         _mouseX.stringValue = [NSString stringWithFormat:@"%.2f", mouseLocation.x];
  62.         _mouseY.stringValue = [NSString stringWithFormat:@"%.2f", mouseLocation.y];
  63.        
  64.         CGPoint carbonPoint = [UIElementUtilities carbonScreenPointFromCocoaScreenPoint:mouseLocation];
  65.         AXUIElementRef elementUnderCursor = NULL;
  66.         // grab what is under the current mouse location
  67.         accessibilityErrorCode = AXUIElementCopyElementAtPosition(_systemWideAccessibilityObject, carbonPoint.x, carbonPoint.y, &elementUnderCursor);
  68.        
  69.         NSArray *array = nil; //  = [NSArray array];
  70.         AXUIElementCopyAttributeNames(elementUnderCursor, (CFArrayRef *)&array);
  71.  
  72.         if (accessibilityErrorCode == kAXErrorSuccess) {
  73.             NSLog(@"Accessibility Code: %d", accessibilityErrorCode);
  74.             NSLog(@"%@", CFCopyDescription(elementUnderCursor));
  75.  
  76.             // print something about the UI element
  77.         } else {
  78.             // we borked
  79.            
  80.             NSLog(@"Accessibility Error Code: %d", accessibilityErrorCode);
  81.             NSLog(@"Description: %@", CFCopyDescription(elementUnderCursor));
  82.         }
  83.    
  84.         lastMouseLocation = mouseLocation;
  85.     }
  86. }
  87.  
  88. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  89.     // Insert code here to tear down your application
  90. }
  91.  
  92.  
  93. /*
  94.  
  95.  kAXErrorSuccess                 = 0,
  96.  kAXErrorFailure                = -25200,
  97.  kAXErrorIllegalArgument            = -25201,
  98.  kAXErrorInvalidUIElement            = -25202,
  99.  kAXErrorInvalidUIElementObserver        = -25203,
  100.  kAXErrorCannotComplete            = -25204,
  101.  kAXErrorAttributeUnsupported        = -25205,
  102.  kAXErrorActionUnsupported            = -25206,
  103.  kAXErrorNotificationUnsupported        = -25207,
  104.  kAXErrorNotImplemented            = -25208,
  105.  kAXErrorNotificationAlreadyRegistered    = -25209,
  106.  kAXErrorNotificationNotRegistered        = -25210,
  107.  kAXErrorAPIDisabled                = -25211,
  108.  kAXErrorNoValue                = -25212,
  109.  kAXErrorParameterizedAttributeUnsupported    = -25213,
  110.  kAXErrorNotEnoughPrecision    = -25214
  111.  
  112.  AXError AXUIElementCopyAttributeNames (AXUIElementRef element, CFArrayRef *names)
  113.  
  114.  Returns a list of all the attributes supported by element
  115.  
  116.  */
  117.  
  118. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement