forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZKAccessibilityElement.m
153 lines (107 loc) · 4.15 KB
/
ZKAccessibilityElement.m
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
#import "ZKAccessibilityElement.h"
@interface ZKAccessibilityElement ()
@property (nonatomic) AXUIElementRef element;
@end
#pragma mark -
@implementation ZKAccessibilityElement
- (id)init {
if (self = [super init]) {
_element = NULL;
}
return self;
}
#pragma mark -
+ (ZKAccessibilityElement *)systemWideElement {
ZKAccessibilityElement *newElement = [ZKAccessibilityElement new];
AXUIElementRef systemWideElement = AXUIElementCreateSystemWide();
newElement.element = systemWideElement;
CFRelease(systemWideElement);
return newElement;
}
#pragma mark -
+ (ZKAccessibilityElement *)frontMostWindowElement {
ZKAccessibilityElement *systemWideElement = ZKAccessibilityElement.systemWideElement;
ZKAccessibilityElement *applicationWithFocusElement = [systemWideElement elementWithAttribute: kAXFocusedApplicationAttribute];
ZKAccessibilityElement *frontMostWindowElement = nil;
if (applicationWithFocusElement) {
frontMostWindowElement = [applicationWithFocusElement elementWithAttribute: kAXFocusedWindowAttribute];
if (!frontMostWindowElement) {
NSLog(@"Invalid accessibility element provided, unable to determine the size and position of the window.");
}
} else {
NSLog(@"Failed to find the application that currently has focus.");
}
return frontMostWindowElement;
}
+ (NSString *)frontMostApplicationName {
ZKAccessibilityElement *systemWideElement = ZKAccessibilityElement.systemWideElement;
ZKAccessibilityElement *applicationWithFocusElement = [systemWideElement elementWithAttribute: kAXFocusedApplicationAttribute];
return [applicationWithFocusElement stringValueOfAttribute: kAXTitleAttribute];
}
#pragma mark -
- (ZKAccessibilityElement *)elementWithAttribute: (CFStringRef)attribute {
ZKAccessibilityElement *newElement = nil;
AXUIElementRef childElement;
AXError result;
result = AXUIElementCopyAttributeValue(_element, attribute, (CFTypeRef *)&childElement);
if (result == kAXErrorSuccess) {
newElement = [ZKAccessibilityElement new];
newElement.element = childElement;
CFRelease(childElement);
} else {
NSLog(@"Unable to obtain the accessibility element with the specified attribute: %@", attribute);
}
return newElement;
}
#pragma mark -
- (NSString *)stringValueOfAttribute: (CFStringRef)attribute {
if (CFGetTypeID(_element) == AXUIElementGetTypeID()) {
CFTypeRef value;
AXError result;
result = AXUIElementCopyAttributeValue(_element, attribute, &value);
if (result == kAXErrorSuccess) {
return CFBridgingRelease(value);
} else {
NSLog(@"There was a problem getting the string value of the specified attribute: %@", attribute);
}
}
return nil;
}
- (AXValueRef)valueOfAttribute: (CFStringRef)attribute type: (AXValueType)type {
if (CFGetTypeID(_element) == AXUIElementGetTypeID()) {
CFTypeRef value;
AXError result;
result = AXUIElementCopyAttributeValue(_element, attribute, (CFTypeRef *)&value);
if ((result == kAXErrorSuccess) && (AXValueGetType(value) == type)) {
return value;
} else {
NSLog(@"There was a problem getting the value of the specified attribute: %@", attribute);
}
}
return NULL;
}
#pragma mark -
- (void)setValue: (AXValueRef)value forAttribute: (CFStringRef)attribute {
AXError result = AXUIElementSetAttributeValue(_element, attribute, (CFTypeRef *)value);
if (result != kAXErrorSuccess) {
NSLog(@"There was a problem setting the value of the specified attribute: %@", attribute);
}
}
#pragma mark -
- (BOOL)isSheet {
return[[self stringValueOfAttribute: kAXRoleAttribute] isEqualToString: (__bridge NSString *)kAXSheetRole];
}
#pragma mark -
- (void)dealloc {
if (_element != NULL) {
CFRelease(_element);
}
}
#pragma mark -
- (void)setElement: (AXUIElementRef)element {
if (_element != NULL) {
CFRelease(_element);
}
_element = CFRetain(element);
}
@end