Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 390 lines (319 sloc) 12.599 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
//
// QCUIElement.m
// QuickCursor
//
// Created by Jesse Grosjean on 11/28/07.
// Copyright 2007 Hog Bay Software. All rights reserved.
//

#import "QCUIElement.h"
#import "NSPasteboard_Extensions.h"

@implementation QCUIElement

#pragma mark Class Methods

+ (QCUIElement *)systemWideElement {
static QCUIElement* systemWideQCUIElement = nil;
if (!systemWideQCUIElement) {
systemWideQCUIElement = [[QCUIElement alloc] initWithAXUIElementRef:AXUIElementCreateSystemWide()];
}
return systemWideQCUIElement;
}

+ (QCUIElement *)focusedElement {
return [[self systemWideElement] valueForAttribute:(NSString *)kAXFocusedUIElementAttribute];
}

#pragma mark Init

- (id)initWithAXUIElementRef:(AXUIElementRef)aUIElementRef {
if (self = [super init]) {
uiElementRef = CFRetain(aUIElementRef);
}
return self;
}

- (void)dealloc {
CFRelease(uiElementRef);
[super dealloc];
}

#pragma mark Attributes

- (pid_t)processID {
pid_t theAppPID = 0;
if (AXUIElementGetPid(uiElementRef, &theAppPID) == kAXErrorSuccess) {
return theAppPID;
}
return -1;
}

- (NSString *)processName {
pid_t theAppPID = 0;
ProcessSerialNumber theAppPSN = {0,0};
NSString * theAppName = NULL;

if (AXUIElementGetPid(uiElementRef, &theAppPID) == kAXErrorSuccess
&& GetProcessForPID(theAppPID, &theAppPSN) == noErr
&& CopyProcessName(&theAppPSN, (CFStringRef *)&theAppName) == noErr) {
return theAppName;
}

return nil;
}

- (QCUIElement *)application {
QCUIElement *uiElement = self;
while (uiElement && ![[uiElement role] isEqualToString:(NSString *)kAXApplicationRole]) {
uiElement = uiElement.parent;
}
return uiElement;
}

- (QCUIElement *)menuBar {
return [[self application] valueForAttribute:(NSString *)kAXMenuBarAttribute];
}

- (QCUIElement *)topLevelUIElement {
return [self valueForAttribute:(NSString *)kAXTopLevelUIElementAttribute];
}

- (QCUIElement *)window {
return [self valueForAttribute:(NSString *)kAXWindowAttribute];
}

- (QCUIElement *)parent {
return [self valueForAttribute:(NSString *)kAXParentAttribute];
}

- (NSArray *)children {
return [self valueForAttribute:(NSString *)kAXChildrenAttribute];
}

- (NSString *)title {
return [self valueForAttribute:(NSString *)kAXTitleAttribute];
}

- (NSString *)role {
return [self valueForAttribute:(NSString *)kAXRoleAttribute];
}

- (BOOL)enabled {
return [[self valueForAttribute:(NSString *)kAXEnabledAttribute] boolValue];
}

- (BOOL)isEditableTextArea {
CFTypeRef focusedAttribute;
AXError error = AXUIElementCopyAttributeValue(uiElementRef, kAXFocusedAttribute, &focusedAttribute);
if (error != kAXErrorSuccess) {
return NO;
}
if (!CFBooleanGetValue(focusedAttribute)) return NO;
NSString *role = [self role];
return [role isEqualToString:(NSString *)kAXTextAreaRole] || [role isEqualToString:(NSString *)kAXTextFieldRole] || [role isEqualToString:@"AXWebArea"];
}

- (id)value {
return [self valueForAttribute:(NSString *)kAXValueAttribute];
}

- (BOOL)setValue:(id)value {
return [self setValue:value forAttribute:(NSString *)kAXValueAttribute];
}

- (NSArray *)attributeNames {
NSArray* attributeNames;
AXUIElementCopyAttributeNames(uiElementRef, (CFArrayRef *)&attributeNames);
return [(id)attributeNames autorelease];
}

- (id)valueForAttribute:(NSString *)attributeName {
id result = nil;
CFTypeRef theValue;

AXError error = AXUIElementCopyAttributeValue(uiElementRef, (CFStringRef)attributeName, &theValue);

if (error != kAXErrorSuccess) {
return nil;
}

if (AXValueGetType(theValue) == kAXValueCGPointType) {
NSLog(@"unimplemented, should not be used by QuickCursor");
} else if (AXValueGetType(theValue) == kAXValueCGSizeType) {
NSLog(@"unimplemented, should not be used by QuickCursor");
} else if (AXValueGetType(theValue) == kAXValueCGRectType) {
NSLog(@"unimplemented, should not be used by QuickCursor");
} else if (AXValueGetType(theValue) == kAXValueCFRangeType) {
NSLog(@"unimplemented, should not be used by QuickCursor");
} else if (CFGetTypeID(theValue) == CFBooleanGetTypeID()) {
result = [NSNumber numberWithBool:theValue == kCFBooleanTrue];
} else if (CFGetTypeID(theValue) == AXUIElementGetTypeID()) {
result = [[[QCUIElement alloc] initWithAXUIElementRef:theValue] autorelease];
} else if (CFGetTypeID(theValue) == CFArrayGetTypeID()) {
CFIndex count = CFArrayGetCount(theValue);
NSMutableArray *children = [NSMutableArray arrayWithCapacity:count];
for (CFIndex i = 0; i < count; i++) {
[children addObject:[[[QCUIElement alloc] initWithAXUIElementRef:CFArrayGetValueAtIndex(theValue, i)] autorelease]];
}
result = children;
} else {
result = [[[(id)theValue description] copy] autorelease];
}

CFRelease(theValue);

return result;

/*
if (theValue) {
if (AXValueGetType(theValue) != kAXValueIllegalType) {
NSLog(@"unimplemented, should not be used by QuickCursor");
} else if (CFGetTypeID(theValue) == CFArrayGetTypeID()) {
NSLog(@"unimplemented, should not be used by QuickCursor");
} else if (CFGetTypeID(theValue) == AXUIElementGetTypeID()) {
return [[QCUIElement alloc] initWithAXUIElementRef:theValue];
} else {
return [(id)theValue description];
}
}
*/
}

- (BOOL)setValue:(id)newValue forAttribute:(NSString *)attributeName {
Boolean settableFlag = false;

AXUIElementIsAttributeSettable(uiElementRef, (CFStringRef)attributeName, &settableFlag);

if (!settableFlag) {
NSLog(@"%@ is not a writeable attribute", attributeName);
return NO;
}

CFTypeRef theOldValue = NULL;
CFTypeRef theNewValue = NULL;

AXError error = AXUIElementCopyAttributeValue(uiElementRef, (CFStringRef)attributeName, &theOldValue);

if (error != kAXErrorSuccess) {
NSLog(@"error in AXUIElementCopyAttributeValue for attribute %@", attributeName);
return NO;
}

if (theOldValue) {
        /*if (AXValueGetType(theOldValue) == kAXValueCGPointType) { // CGPoint
CGPoint point;
theNewValue = AXValueCreate(kAXValueCGPointType, (const void *)&point);
if (theNewValue) {
error = AXUIElementSetAttributeValue(uiElementRef, (CFStringRef)attributeName, theNewValue );
CFRelease(theNewValue);
}
} else if (AXValueGetType(theOldValue) == kAXValueCGSizeType) { // CGSize
CGSize size;
theNewValue = AXValueCreate( kAXValueCGSizeType, (const void *)&size );
if (theNewValue) {
error = AXUIElementSetAttributeValue(uiElementRef, (CFStringRef)attributeName, theNewValue );
CFRelease( theNewValue );
}
} else if (AXValueGetType(theOldValue) == kAXValueCGRectType) { // CGRect
CGRect rect;
theNewValue = AXValueCreate( kAXValueCGRectType, (const void *)&rect );
if (theNewValue) {
error = AXUIElementSetAttributeValue(uiElementRef, (CFStringRef)attributeName, theNewValue );
CFRelease( theNewValue );
}
} else if (AXValueGetType(theOldValue) == kAXValueCFRangeType) { // CFRange
CFRange range;
theNewValue = AXValueCreate( kAXValueCFRangeType, (const void *)&range );
if (theNewValue) {
error = AXUIElementSetAttributeValue(uiElementRef, (CFStringRef)attributeName, theNewValue );
CFRelease( theNewValue );
}
} else*/ if (CFGetTypeID(theOldValue) == CFBooleanGetTypeID()) {
if ([newValue boolValue]) {
theNewValue = kCFBooleanTrue;
} else {
theNewValue = kCFBooleanFalse;
}
error = AXUIElementSetAttributeValue(uiElementRef, (CFStringRef)attributeName, theNewValue);
} else if ([(id)theOldValue isKindOfClass:[NSString class]]) { // NSString
            error = AXUIElementSetAttributeValue(uiElementRef, (CFStringRef)attributeName, newValue);
        } else if ([(id)theOldValue isKindOfClass:[NSValue class]]) { // NSValue
            error = AXUIElementSetAttributeValue(uiElementRef, (CFStringRef)attributeName, [NSNumber numberWithLong:[newValue intValue]] );
        }

CFRelease(theOldValue);
}

if (error != kAXErrorSuccess) {
NSLog(@"error in AXUIElementSetAttributeValue for attribute %@", attributeName);
return NO;
}

return YES;
}

#pragma mark Process

- (BOOL)activateProcess {
pid_t theAppPID = 0;
ProcessSerialNumber theAppPSN = {0,0};

if (AXUIElementGetPid(uiElementRef, &theAppPID) == kAXErrorSuccess && GetProcessForPID(theAppPID, &theAppPSN) == noErr && SetFrontProcess(&theAppPSN) == noErr) {
return YES;
}

return NO;
}

- (QCUIElement *)menuItemWithShortCut:(NSString *)shortCut modifiers:(NSString *)modifiers searchingIn:(QCUIElement *)aMenu {
if ([shortCut isEqualToString:[aMenu valueForAttribute:(NSString *)kAXMenuItemCmdCharAttribute]]) {
if ([[aMenu valueForAttribute:(NSString *)kAXMenuItemCmdModifiersAttribute] isEqual:modifiers]) {
return aMenu;
}
}

for (QCUIElement *eachMenuItem in aMenu.children) {
QCUIElement *eachSearched = [self menuItemWithShortCut:shortCut modifiers:modifiers searchingIn:eachMenuItem];
if (eachSearched) {
return eachSearched;
}
}

return nil;
}

- (QCUIElement *)menuItemWithShortCut:(NSString *)shortCut modifiers:(NSString *)modifiers {
QCUIElement *menuBar = [self menuBar];
NSArray *menuBarItems = [menuBar children];
QCUIElement *editMenu = [[[menuBarItems objectAtIndex:3] children] lastObject];
QCUIElement *found = [self menuItemWithShortCut:shortCut modifiers:modifiers searchingIn:editMenu];

if (!found) {
found = [self menuItemWithShortCut:shortCut modifiers:modifiers searchingIn:menuBar];
}

return found;
}

- (BOOL)performSelectAll {
QCUIElement *selectAllMenuItem = [self menuItemWithShortCut:@"A" modifiers:@"0"];
if ([selectAllMenuItem enabled]) {
return AXUIElementPerformAction(selectAllMenuItem->uiElementRef, kAXPressAction) == kAXErrorSuccess;
} else {
return NO;
}
}

- (NSString *)performCopee:(BOOL)trySelectAllIfFail {
QCUIElement *copyMenuItem = [self menuItemWithShortCut:@"C" modifiers:@"0"]; // seems neccesary to either refresh or wait for enabled status to update.

if (copyMenuItem) {
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
NSUInteger changeCount = [pboard changeCount];
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];

if (AXUIElementPerformAction(copyMenuItem->uiElementRef, kAXPressAction) == kAXErrorSuccess) {
while ([pboard changeCount] == changeCount) {
if (([NSDate timeIntervalSinceReferenceDate] - startTime) > 0.3) {
if (trySelectAllIfFail) {
[self performSelectAll];
return [self performCopee:NO];
} else {
return @"";
}
}
usleep(100000);
}
return [pboard stringForType:NSPasteboardTypeString];
}

return @"";
} else {
return nil;
}
}

- (NSString *)readString {
QCUIElement *copyMenuItem = [self menuItemWithShortCut:@"C" modifiers:@"0"];

if (copyMenuItem) {
if (![copyMenuItem enabled]) {
if (![self performSelectAll]) {
return NO;
}
}

NSPasteboard *pboard = [NSPasteboard generalPasteboard];
        NSDictionary *savedContents = [pboard savePasteboardContents];
        
//NSString *savedContents = [pboard stringForType:NSPasteboardTypeString];
NSString *copiedContents = [self performCopee:YES];
        [pboard restorePasteboardContents:savedContents];
        //[pboard clearContents];
//[pboard setString:savedContents forType:NSPasteboardTypeString]; // trying to restore original clip board contents... doesn't seem to work, not sure if good idaea anyway.
return copiedContents;
}

return nil;
}

- (BOOL)writeString:(NSString *)pasteString {
NSPasteboard *pboard = [NSPasteboard generalPasteboard];

    //NSString *savedContents = [pboard stringForType:NSPasteboardTypeString];
    NSDictionary *savedContents = [pboard savePasteboardContents];

[pboard clearContents];
[pboard declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:nil];
[pboard setString:pasteString forType:NSPasteboardTypeString];

if (![self activateProcess]) {
return NO;
}

QCUIElement *pasteMenuItem = [self menuItemWithShortCut:@"V" modifiers:@"0"];

if ([pasteMenuItem enabled]) {
if (AXUIElementPerformAction(pasteMenuItem->uiElementRef, kAXPressAction) == kAXErrorSuccess) {
            usleep(100000); // hack... might not work with long documents?
            [pboard restorePasteboardContents:savedContents];
            //[pboard clearContents];
            //[pboard setString:savedContents forType:NSPasteboardTypeString];
return YES;
}
}
    
return NO;
}

@end
Something went wrong with that request. Please try again.