Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 233 lines (186 sloc) 7.002 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
//
// QCUIElement.m
// QuickCursor
//
// Created by Jesse Grosjean on 11/28/07.
// Copyright 2007 Hog Bay Software. All rights reserved.
//

#import "QCUIElement.h"


@implementation QCUIElement

#pragma mark Class Methods

// kAXValueChangedNotification
// kAXUIElementDestroyedNotification

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

+ (QCUIElement *)focusedElement {
return [[self systemWideElement] valueForAttribute:@"AXFocusedUIElement"];
}

#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.topLevelUIElement;
while (uiElement && ![[uiElement role] isEqualToString:(NSString *)kAXApplicationRole]) {
uiElement = uiElement.parent;
}
return uiElement;
}

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

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

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

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

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

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

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


- (NSArray *)attributeNames {
NSArray* attributeNames;
AXUIElementCopyAttributeNames(uiElementRef, (CFArrayRef *)&attributeNames);
return attributeNames;
}

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

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

if (error != kAXErrorSuccess) {
NSLog(@"error in AXUIElementCopyAttributeValue");
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) == AXUIElementGetTypeID()) {
result = [[[QCUIElement alloc] initWithAXUIElementRef:theValue] autorelease];
} else if (CFGetTypeID(theValue) == CFArrayGetTypeID()) {
NSLog(@"unimplemented, should not be used by QuickCursor");
} 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, nil);
return NO;
}

CFTypeRef theOldValue = NULL;
CFTypeRef theNewValue = NULL;

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

if (error != kAXErrorSuccess) {
NSLog(@"error in AXUIElementCopyAttributeValue");
return NO;
}

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

CFRelease(theOldValue);
}

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;
}

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