-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIElementUtilitiesAdditions.m
374 lines (231 loc) · 10.8 KB
/
UIElementUtilitiesAdditions.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
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
//
// UIElementUtilitiesAdditions.m
// AXRecord
//
// Created by Sylvain Malacria on 15/03/16.
// Copyright © 2016 Sylvain Malacria. All rights reserved.
//
#import "UIElementUtilitiesAdditions.h"
#import "UIElementUtilities.h"
@implementation UIElementUtilitiesAdditions
// -------------------------------------------------------------------------------
// stringDescriptionOfRelevantUIElementsBeingVerbose:
//
// Return a descriptive string of attributes and actions of a given uiElement.
// and all his relevant children
// is consider relevant if interactive, big enough, visible, etc.
// -------------------------------------------------------------------------------
+ (NSString *)stringDescriptionOfRelevantUIElementsBeingVerbose:(AXUIElementRef)element
{
NSMutableString * theDescriptionStr = [NSMutableString new] ;
NSArray * theNames;
CFIndex nameIndex;
CFIndex numOfNames;
// display actions
NSArray * theActions = [UIElementUtilities actionNamesOfUIElement:element];
// Actions should contain the name of action so check weither or not this is relevant
// and instantiate the value of isRelevant with correct value
BOOL isRelevant= [UIElementUtilitiesAdditions isRelevantActionContainedInArrayOfActions:theActions];
NSString* elementRole = [UIElementUtilities roleOfUIElement:element];
if(!isRelevant){
// element role is the role of current element
// test wether or not the role is relevant
// and update the value of isrelevant
isRelevant = [UIElementUtilitiesAdditions isRelevantRole:elementRole];
}
if(isRelevant){
//TODO: here we should do what we have to do.
// either store the interesting information in a string (this one is dummy)
NSString* dummyString = [NSString stringWithFormat:@"%@",elementRole];
// and append it to the theDescriptionStr
[theDescriptionStr appendFormat:@"%@",elementRole];
for(NSString* action in theActions){
[theDescriptionStr appendFormat:@"-%@",action];
}
// or write the log somewhere
[theDescriptionStr appendFormat:@"\n"];
}
//todo change with test whether there is a visiblechildren attribute or not
BOOL hasVisibleChildren = YES;
NSArray* visibleChildren = [UIElementUtilities valueOfAttribute:NSAccessibilityVisibleChildrenAttribute ofUIElement:element];
if(visibleChildren){
if([visibleChildren count]==0){
hasVisibleChildren=NO;
}
}
if(hasVisibleChildren){
CFTypeRef children = (__bridge AXUIElementRef)[UIElementUtilities valueOfAttribute:NSAccessibilityVisibleChildrenAttribute ofUIElement:element];
if(children){
if(CFGetTypeID(children)==CFArrayGetTypeID()){
CFIndex count = CFArrayGetCount(children);
if(count>0){
for( CFIndex i = 0; i < count; i++ )
{
AXUIElementRef child = (AXUIElementRef) CFArrayGetValueAtIndex(children,i);
[theDescriptionStr appendFormat:@"%@",[UIElementUtilitiesAdditions stringDescriptionOfRelevantUIElementsBeingVerbose:child]];
}
}
}
}
}
//do the same for visible rows and visible columns
return theDescriptionStr;
}
#pragma mark -
#pragma mark Custom methods for writing what's interesting
// -------------------------------------------------------------------------------
// isRelevantRole:
//
// Return a BOOL corresponding to YES if the role is considered "relevant"
// -------------------------------------------------------------------------------
+(BOOL)isRelevantRole:(NSString*)elementRole{
BOOL result =NO;
//TODO: complete this method with the actual relevant role
result = ([elementRole rangeOfString:@"Button"].length != 0);
//result = [elementRole containsString:@"Button"]; // requires OSX 10.10+
result = [elementRole isEqualToString:@"Text"];
result = [elementRole isEqualToString:@"AXButton"];
result = [elementRole isEqualToString:@"AXButton"];
return result;
}
// -------------------------------------------------------------------------------
// isRelevantActionContainedInArrayOfActions:
//
// Return a BOOL corresponding to YES if the ARRAY contains one of the action considered "relevant"
// -------------------------------------------------------------------------------
+(BOOL) isRelevantActionContainedInArrayOfActions:(NSArray*)listOfActions
{
//TODO: complete this method with the actual relevant actions
BOOL result = NO;
return result;
}
+(NSString*)relevantXMLDescriptionOfElement:(AXUIElementRef)element{
return [UIElementUtilitiesAdditions relevantXMLDescriptionOfElement:element beingSelfClosed:NO];
}
+(NSString*)relevantXMLDescriptionOfElement:(AXUIElementRef)element beingSelfClosed:(BOOL)selfclosed{
NSMutableString* result;
if(element){
result= [NSMutableString new];
[result appendFormat:@"<%@ ",[UIElementUtilities roleOfUIElement:element]];
NSRect frame = [UIElementUtilities frameOfUIElement:element];
// if frame exists
if(!NSEqualRects(frame, NSZeroRect)){
[result appendFormat:@"x=%.0f y=%.0f w=%.0f h=%.0f ",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height];
}
//if contains value info
id value = [UIElementUtilities valueOfAttribute:NSAccessibilityValueAttribute ofUIElement:element];
if(value){
[result appendFormat:@"value=%@ ",value];
}
// if contains focused info
value = [UIElementUtilities valueOfAttribute:NSAccessibilityFocusedAttribute ofUIElement:element];
if(value){
[result appendFormat:@"focused=%@ ",value];
}
//if contains enabled info
value = [UIElementUtilities valueOfAttribute:NSAccessibilityEnabledAttribute ofUIElement:element];
if(value){
[result appendFormat:@"enabled=%@ ",value];
}
// if contains title info
value = [UIElementUtilities valueOfAttribute:NSAccessibilityTitleAttribute ofUIElement:element];
if(value){
[result appendFormat:@"title=%@ ",value];
}
// if contains label info
value = [UIElementUtilities valueOfAttribute:NSAccessibilityLabelValueAttribute ofUIElement:element];
if(value){
[result appendFormat:@"label=%@ ",value];
}
//if contains help info
value = [UIElementUtilities valueOfAttribute:NSAccessibilityHelpAttribute ofUIElement:element];
if(value){
[result appendFormat:@"help=%@ ",value];
}
if(selfclosed){
[result appendString:@"/"];
}
[result appendString:@">\n"];
}
return result;
}
+(NSString*)XMLDescriptionOfElement:(AXUIElementRef)element{
return [UIElementUtilitiesAdditions XMLDescriptionOfElement:element beingSelfClosed:NO];
}
+ (NSString *)XMLDescriptionOfElement:(AXUIElementRef)element beingSelfClosed:(BOOL)selfclosed
{
NSMutableString * theDescriptionStr = [NSMutableString new] ;
NSArray * theNames;
CFIndex nameIndex;
CFIndex numOfNames;
[theDescriptionStr appendFormat:@"<%@ ",[UIElementUtilities roleOfUIElement:element]];
// display attributes
theNames = [UIElementUtilities attributeNamesOfUIElement:element];
if (theNames) {
numOfNames = [theNames count];
for( nameIndex = 0; nameIndex < numOfNames; nameIndex++ ) {
NSString * theName = NULL;
// Grab name
theName = [theNames objectAtIndex:nameIndex];
// Add string
[theDescriptionStr appendFormat:@"%@='%@' ", theName, [UIElementUtilities descriptionForUIElement:element attribute:theName beingVerbose:false]];
}
}
// display actions
theNames = [UIElementUtilities actionNamesOfUIElement:element];
if (theNames) {
numOfNames = [theNames count];
for( nameIndex = 0; nameIndex < numOfNames; nameIndex++ ) {
NSString * theName = NULL;
NSString * theDesc = NULL;
// Grab name
theName = [theNames objectAtIndex:nameIndex];
// Grab description
theDesc = [UIElementUtilities descriptionOfAction:theName ofUIElement:element];
// Add string
[theDescriptionStr appendFormat:@"%@='%@' ", theName, theDesc];
}
}
if(selfclosed){
[theDescriptionStr appendString:@"/"];
}
[theDescriptionStr appendString:@">\n"];
return theDescriptionStr;
}
// -------------------------------------------------------------------------------
// elementAtCGPoint: withSystemWideElement:
// -------------------------------------------------------------------------------
+ (AXUIElementRef)elementAtCGPoint:(CGPoint)pointAsCGPoint withSystemWideElement:(AXUIElementRef)systemWideElement
{
AXUIElementRef newElement = NULL;
// Ask Accessibility API for UI Element under the mouse
if (AXUIElementCopyElementAtPosition( systemWideElement, pointAsCGPoint.x, pointAsCGPoint.y, &newElement ) == kAXErrorSuccess)
{
}
return newElement;
}
// -------------------------------------------------------------------------------
// arrayOfAncestorsOfElement:element
//
// Return an array with all ancestors in it
// -------------------------------------------------------------------------------
+(NSArray*)arrayOfAncestorsOfElement:(AXUIElementRef)element{
NSArray *lineage = [NSArray array];
AXUIElementRef parent = (__bridge AXUIElementRef)[UIElementUtilities valueOfAttribute:NSAccessibilityParentAttribute ofUIElement:element];
if (parent != NULL) {
lineage = [self arrayOfAncestorsOfElement:parent];
}
return [lineage arrayByAddingObject:(__bridge id)element];
}
/**
* method checks whether or not the AXUIelementRef has an attribute
* for the given attribute
* return yes if true, no otherwise
@param AXUIElementRef the element we are interested in
@return BOOL yes if the element has the given attribute
*/
+(BOOL)hasGivenElement:(AXUIElementRef)element attribute:(NSString*)attribute{
id attributeValue = [UIElementUtilities valueOfAttribute:attribute ofUIElement:element];
return (attributeValue!=nil);
}
@end