forked from atoxx/DockMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIElementUtilities.m
executable file
·542 lines (437 loc) · 21.1 KB
/
UIElementUtilities.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
File: UIElementUtilities.m
Abstract: Utility methods to manage AXUIElementRef instances.
Version: 1.4
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2010 Apple Inc. All Rights Reserved.
*/
#import "UIElementUtilities.h"
NSString *const UIElementUtilitiesNoDescription = @"No Description";
@implementation UIElementUtilities
#pragma mark -
+ (pid_t)processIdentifierOfUIElement:(AXUIElementRef)element {
pid_t pid = 0;
if (AXUIElementGetPid (element, &pid) == kAXErrorSuccess) {
return pid;
} else {
return 0;
}
}
+ (NSArray *)attributeNamesOfUIElement:(AXUIElementRef)element {
NSArray *attrNames = nil;
AXUIElementCopyAttributeNames(element, (void *)&attrNames);
return attrNames;
}
+ (NSArray *)actionNamesOfUIElement:(AXUIElementRef)element {
NSArray *actionNames = nil;
AXUIElementCopyActionNames(element, (void *)&actionNames);
return actionNames;
}
+ (NSString *)descriptionOfAction:(NSString *)actionName ofUIElement:(AXUIElementRef)element {
NSString *actionDescription = nil;
AXUIElementCopyActionDescription(element, (__bridge CFStringRef)actionName, (void *)&actionDescription);
return actionDescription;
}
+ (void)performAction:(NSString *)actionName ofUIElement:(AXUIElementRef)element {
AXUIElementPerformAction( element, (__bridge CFStringRef)actionName);
}
// -------------------------------------------------------------------------------
// valueOfExistingAttribute:attribute:element
//
// Given a uiElement and its attribute, return the value of an accessibility object's attribute.
// -------------------------------------------------------------------------------
+ (id)valueOfAttribute:(NSString *)attribute ofUIElement:(AXUIElementRef)element
{
id result = nil;
NSArray *attributeNames = [UIElementUtilities attributeNamesOfUIElement:element];
if (attributeNames) {
if ( [attributeNames indexOfObject:(NSString *)attribute] != NSNotFound
&&
AXUIElementCopyAttributeValue(element, (__bridge CFStringRef)attribute, (void *)&result) == kAXErrorSuccess
) {
// [result autorelease];
}
}
return result;
}
+ (BOOL)canSetAttribute:(NSString *)attributeName ofUIElement:(AXUIElementRef)element {
Boolean isSettable = false;
AXUIElementIsAttributeSettable(element, (__bridge CFStringRef)attributeName, &isSettable);
return (BOOL)isSettable;
}
+ (void)setStringValue:(NSString *)stringValue forAttribute:(NSString *)attributeName ofUIElement:(AXUIElementRef)element;
{
CFTypeRef theCurrentValue = NULL;
// First, found out what type of value it is.
if ( attributeName
&& AXUIElementCopyAttributeValue( element, (__bridge CFStringRef)attributeName, &theCurrentValue ) == kAXErrorSuccess
&& theCurrentValue) {
CFTypeRef valueRef = NULL;
// Set the value using based on the type
if (AXValueGetType(theCurrentValue) == kAXValueCGPointType) { // CGPoint
float x, y;
sscanf( [stringValue UTF8String], "x=%g y=%g", &x, &y );
CGPoint point = CGPointMake(x, y);
valueRef = AXValueCreate( kAXValueCGPointType, (const void *)&point );
if (valueRef) {
AXUIElementSetAttributeValue( element, (__bridge CFStringRef)attributeName, valueRef );
CFRelease( valueRef );
}
}
else if (AXValueGetType(theCurrentValue) == kAXValueCGSizeType) { // CGSize
float w, h;
sscanf( [stringValue UTF8String], "w=%g h=%g", &w, &h );
CGSize size = CGSizeMake(w, h);
valueRef = AXValueCreate( kAXValueCGSizeType, (const void *)&size );
if (valueRef) {
AXUIElementSetAttributeValue( element, (__bridge CFStringRef)attributeName, valueRef );
CFRelease( valueRef );
}
}
else if (AXValueGetType(theCurrentValue) == kAXValueCGRectType) { // CGRect
float x, y, w, h;
sscanf( [stringValue UTF8String], "x=%g y=%g w=%g h=%g", &x, &y, &w, &h );
CGRect rect = CGRectMake(x, y, w, h);
valueRef = AXValueCreate( kAXValueCGRectType, (const void *)&rect );
if (valueRef) {
AXUIElementSetAttributeValue( element, (__bridge CFStringRef)attributeName, valueRef );
CFRelease( valueRef );
}
}
else if (AXValueGetType(theCurrentValue) == kAXValueCFRangeType) { // CFRange
CFRange range;
sscanf( [stringValue UTF8String], "pos=%ld len=%ld", &(range.location), &(range.length) );
valueRef = AXValueCreate( kAXValueCFRangeType, (const void *)&range );
if (valueRef) {
AXUIElementSetAttributeValue( element, (__bridge CFStringRef)attributeName, valueRef );
CFRelease( valueRef );
}
}
else if ([(__bridge id)theCurrentValue isKindOfClass:[NSString class]]) { // NSString
AXUIElementSetAttributeValue( element, (__bridge CFStringRef)attributeName, (__bridge CFTypeRef)(stringValue) );
}
else if ([(__bridge id)theCurrentValue isKindOfClass:[NSValue class]]) { // NSValue
AXUIElementSetAttributeValue( element, (__bridge CFStringRef)attributeName, (__bridge CFTypeRef)([NSNumber numberWithFloat:[stringValue floatValue]]) );
}
}
}
+ (AXUIElementRef)parentOfUIElement:(AXUIElementRef)element {
return (__bridge AXUIElementRef)[UIElementUtilities valueOfAttribute:NSAccessibilityParentAttribute ofUIElement:element];
}
+ (NSString *)roleOfUIElement:(AXUIElementRef)element {
return (NSString *)[UIElementUtilities valueOfAttribute:NSAccessibilityRoleAttribute ofUIElement:element];
}
+ (NSString *)titleOfUIElement:(AXUIElementRef)element {
return (NSString *)[UIElementUtilities valueOfAttribute:NSAccessibilityTitleAttribute ofUIElement:element];
}
+ (BOOL)isApplicationUIElement:(AXUIElementRef)element {
return [[UIElementUtilities roleOfUIElement:element] isEqualToString:NSAccessibilityApplicationRole];
}
#pragma mark -
// Flip coordinates
+ (CGPoint)carbonScreenPointFromCocoaScreenPoint:(NSPoint)cocoaPoint {
NSScreen *foundScreen = nil;
CGPoint thePoint;
for (NSScreen *screen in [NSScreen screens]) {
if (NSPointInRect(cocoaPoint, [screen frame])) {
foundScreen = screen;
}
}
if (foundScreen) {
CGFloat screenHeight = [foundScreen frame].size.height;
thePoint = CGPointMake(cocoaPoint.x, screenHeight - cocoaPoint.y - 1);
} else {
thePoint = CGPointMake(0.0, 0.0);
}
return thePoint;
}
// -------------------------------------------------------------------------------
// FlippedScreenBounds:bounds
// -------------------------------------------------------------------------------
+ (NSRect) flippedScreenBounds:(NSRect) bounds
{
float screenHeight = NSMaxY([[[NSScreen screens] objectAtIndex:0] frame]);
bounds.origin.y = screenHeight - NSMaxY(bounds);
return bounds;
}
+ (NSRect)frameOfUIElement:(AXUIElementRef)element {
NSRect bounds = NSZeroRect;
id elementPosition = [UIElementUtilities valueOfAttribute:NSAccessibilityPositionAttribute ofUIElement:element];
id elementSize = [UIElementUtilities valueOfAttribute:NSAccessibilitySizeAttribute ofUIElement:element];
if (elementPosition && elementSize) {
NSRect topLeftWindowRect;
AXValueGetValue((__bridge AXValueRef)elementPosition, kAXValueCGPointType, &topLeftWindowRect.origin);
AXValueGetValue((__bridge AXValueRef)elementSize, kAXValueCGSizeType, &topLeftWindowRect.size);
bounds = [self flippedScreenBounds:topLeftWindowRect];
}
return bounds;
}
#pragma mark -
#pragma mark String Descriptions
// -------------------------------------------------------------------------------
// stringDescriptionOfAXValue:valueRef:beVerbose
//
// Returns a descriptive string according to the values' structure type.
// -------------------------------------------------------------------------------
+ (NSString *)stringDescriptionOfAXValue:(CFTypeRef)valueRef beingVerbose:(BOOL)beVerbose
{
NSString *result = @"AXValue???";
switch (AXValueGetType(valueRef)) {
case kAXValueCGPointType: {
CGPoint point;
if (AXValueGetValue(valueRef, kAXValueCGPointType, &point)) {
if (beVerbose)
result = [NSString stringWithFormat:@"<AXPointValue x=%g y=%g>", point.x, point.y];
else
result = [NSString stringWithFormat:@"x=%g y=%g", point.x, point.y];
}
break;
}
case kAXValueCGSizeType: {
CGSize size;
if (AXValueGetValue(valueRef, kAXValueCGSizeType, &size)) {
if (beVerbose)
result = [NSString stringWithFormat:@"<AXSizeValue w=%g h=%g>", size.width, size.height];
else
result = [NSString stringWithFormat:@"w=%g h=%g", size.width, size.height];
}
break;
}
case kAXValueCGRectType: {
CGRect rect;
if (AXValueGetValue(valueRef, kAXValueCGRectType, &rect)) {
if (beVerbose)
result = [NSString stringWithFormat:@"<AXRectValue x=%g y=%g w=%g h=%g>", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height];
else
result = [NSString stringWithFormat:@"x=%g y=%g w=%g h=%g", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height];
}
break;
}
case kAXValueCFRangeType: {
CFRange range;
if (AXValueGetValue(valueRef, kAXValueCFRangeType, &range)) {
if (beVerbose)
result = [NSString stringWithFormat:@"<AXRangeValue pos=%ld len=%ld>", range.location, range.length];
else
result = [NSString stringWithFormat:@"pos=%ld len=%ld", range.location, range.length];
}
break;
}
default:
break;
}
return result;
}
// -------------------------------------------------------------------------------
// descriptionOfValue:theValue:beVerbose
//
// Called from "descriptionForUIElement", return a descripting string (role and title)
// of the given value (AXUIElementRef).
// -------------------------------------------------------------------------------
+ (NSString *)descriptionOfValue:(CFTypeRef)theValue beingVerbose:(BOOL)beVerbose
{
NSString * theValueDescString = NULL;
if (theValue) {
if (AXValueGetType(theValue) != kAXValueIllegalType) {
theValueDescString = [self stringDescriptionOfAXValue:theValue beingVerbose:beVerbose];
}
else if (CFGetTypeID(theValue) == CFArrayGetTypeID()) {
theValueDescString = [NSString stringWithFormat:@"<array of size %lu>", (unsigned long)[(__bridge NSArray *)theValue count]];
}
else if (CFGetTypeID(theValue) == AXUIElementGetTypeID()) {
NSString * uiElementRole = NULL;
if (AXUIElementCopyAttributeValue( (AXUIElementRef)theValue, kAXRoleAttribute, (void *)&uiElementRole ) == kAXErrorSuccess) {
NSString * uiElementTitle = NULL;
uiElementTitle = [self valueOfAttribute:NSAccessibilityTitleAttribute ofUIElement:(AXUIElementRef)theValue];
#if 0
// hack to work around cocoa app objects not having titles yet
if (uiElementTitle == nil && [uiElementRole isEqualToString:(NSString *)kAXApplicationRole]) {
pid_t theAppPID = 0;
ProcessSerialNumber theAppPSN = {0,0};
NSString * theAppName = NULL;
if (AXUIElementGetPid( (AXUIElementRef)theValue, &theAppPID ) == kAXErrorSuccess
&& GetProcessForPID( theAppPID, &theAppPSN ) == noErr
&& CopyProcessName( &theAppPSN, (CFStringRef *)&theAppName ) == noErr ) {
uiElementTitle = theAppName;
}
}
#endif
if (uiElementTitle != nil) {
theValueDescString = [NSString stringWithFormat:@"<%@: “%@”>", uiElementRole, uiElementTitle];
}
else {
theValueDescString = [NSString stringWithFormat:@"<%@>", uiElementRole];
}
}
else {
theValueDescString = [(__bridge id)theValue description];
}
}
else {
theValueDescString = [(__bridge id)theValue description];
}
}
return theValueDescString;
}
// -------------------------------------------------------------------------------
// lineageOfUIElement:element
//
// Return the lineage array or inheritance of a given uiElement.
// -------------------------------------------------------------------------------
+ (NSArray *)lineageOfUIElement:(AXUIElementRef)element
{
NSArray *lineage = [NSArray array];
NSString *elementDescr = [self descriptionOfValue:element beingVerbose:NO];
AXUIElementRef parent = (__bridge AXUIElementRef)[self valueOfAttribute:NSAccessibilityParentAttribute ofUIElement:element];
if (parent != NULL) {
lineage = [self lineageOfUIElement:parent];
}
return [lineage arrayByAddingObject:elementDescr];
}
// -------------------------------------------------------------------------------
// lineageDescriptionOfUIElement:element
//
// Return the descriptive string of a uiElement's lineage.
// -------------------------------------------------------------------------------
+ (NSString *)lineageDescriptionOfUIElement:(AXUIElementRef)element
{
NSMutableString *result = [NSMutableString string];
NSMutableString *indent = [NSMutableString string];
NSArray *lineage = [self lineageOfUIElement:element];
NSString *ancestor;
NSEnumerator *e = [lineage objectEnumerator];
while (ancestor = [e nextObject]) {
[result appendFormat:@"%@%@\n", indent, ancestor];
[indent appendString:@" "];
}
return result;
}
// -------------------------------------------------------------------------------
// stringDescriptionOfUIElement:inElement
//
// Return a descriptive string of attributes and actions of a given uiElement.
// -------------------------------------------------------------------------------
+ (NSString *)stringDescriptionOfUIElement:(AXUIElementRef)element
{
// NSMutableString * theDescriptionStr = [[NSMutableString new] autorelease];
NSMutableString * theDescriptionStr = [NSMutableString string];
NSArray * theNames;
CFIndex nameIndex;
CFIndex numOfNames;
[theDescriptionStr appendFormat:@"%@", [self lineageDescriptionOfUIElement:element]];
// display attributes
theNames = [UIElementUtilities attributeNamesOfUIElement:element];
if (theNames) {
numOfNames = [theNames count];
if (numOfNames)
[theDescriptionStr appendString:@"\nAttributes:\n"];
for( nameIndex = 0; nameIndex < numOfNames; nameIndex++ ) {
NSString * theName = NULL;
id theValue = NULL;
Boolean theSettableFlag = false;
// Grab name
theName = [theNames objectAtIndex:nameIndex];
// Grab settable field
AXUIElementIsAttributeSettable( element, (__bridge CFStringRef)theName, &theSettableFlag );
// Add string
[theDescriptionStr appendFormat:@" %@%@: “%@”\n", theName, (theSettableFlag?@" (W)":@""), [self descriptionForUIElement:element attribute:theName beingVerbose:false]];
// [theValue release];
}
}
// display actions
theNames = [UIElementUtilities actionNamesOfUIElement:element];
if (theNames) {
numOfNames = [theNames count];
if (numOfNames)
[theDescriptionStr appendString:@"\nActions:\n"];
for( nameIndex = 0; nameIndex < numOfNames; nameIndex++ ) {
NSString * theName = NULL;
NSString * theDesc = NULL;
// Grab name
theName = [theNames objectAtIndex:nameIndex];
// Grab description
theDesc = [self descriptionOfAction:theName ofUIElement:element];
// Add string
[theDescriptionStr appendFormat:@" %@ - %@\n", theName, theDesc];
}
}
return theDescriptionStr;
}
// -------------------------------------------------------------------------------
// descriptionForUIElement:uiElement:beingVerbose
//
// Return a descripting string (role and title) of the given uiElement (AXUIElementRef).
// -------------------------------------------------------------------------------
+ (NSString *)descriptionForUIElement:(AXUIElementRef)uiElement attribute:(NSString *)name beingVerbose:(BOOL)beVerbose
{
NSString * theValueDescString = NULL;
CFTypeRef theValue;
CFIndex count;
if (([name isEqualToString:NSAccessibilityChildrenAttribute]
||
[name isEqualToString:NSAccessibilityRowsAttribute]
)
&&
AXUIElementGetAttributeValueCount(uiElement, (__bridge CFStringRef)name, &count) == kAXErrorSuccess) {
// No need to get the value of large arrays - we just display their size.
// We don't want to do this with every attribute because AXUIElementGetAttributeValueCount on non-array valued
// attributes will cause debug spewage.
theValueDescString = [NSString stringWithFormat:@"<array of size %ld>", count];
} else if (AXUIElementCopyAttributeValue ( uiElement, (__bridge CFStringRef)name, &theValue ) == kAXErrorSuccess && theValue) {
theValueDescString = [self descriptionOfValue:theValue beingVerbose:beVerbose];
}
return theValueDescString;
}
// This method returns a 'no description' string by default
+ (NSString *)descriptionOfAXDescriptionOfUIElement:(AXUIElementRef)element {
id result = [self valueOfAttribute:NSAccessibilityDescriptionAttribute ofUIElement:element];
return (!result || [result isEqualToString:@""]) ? UIElementUtilitiesNoDescription: [result description];
}
#pragma -
#pragma mark My Method
+ (NSDictionary *)attributeDictionaryOfUIElement:(AXUIElementRef)element
{
NSMutableDictionary* attributesDictionary = [@{} mutableCopy];
NSArray* theNames = [UIElementUtilities attributeNamesOfUIElement:element];
if (theNames) {
for (NSString* theName in theNames) {
NSString* description = [self descriptionForUIElement:element attribute:theName beingVerbose:false];
[attributesDictionary setObject:(description != nil) ? description : [NSNull null] forKeyedSubscript:theName];
}
}
return [attributesDictionary copy];
}
@end