Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 246 lines (183 sloc) 6.119 kb
dc5be1c Initial check-in.
uli authored
1 //
2 // UKUIElement.m
3 // TalkingMoose
4 //
5 // Created by Uli Kusterer on 03.07.05.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 // Copyright 2005 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c Initial check-in.
uli authored
26 //
27
28 #import "UKUIElement.h"
29 #import <sys/types.h>
30 #import <unistd.h>
31
32 void UKUIObserverCallback( AXObserverRef observer, AXUIElementRef element, CFStringRef notification, void *refcon );
33
34 @implementation UKUIElement
35
36 +(BOOL) isAccessibilityEnabled
37 {
38 return AXAPIEnabled();
39 }
40
41 +(BOOL) makeSureAccessibilityIsEnabled
42 {
43 BOOL axOn = AXAPIEnabled();
44
45 if( !axOn )
46 {
47 int ret = NSRunAlertPanel( @"This Feature requires that the Accessibility API be enabled.", @"Would you like me to launch System Preferences so that you can turn on \"Enable access for assistive devices\"?",
48 @"OK", @"Quit", @"Cancel");
49
50 switch (ret)
51 {
52 case NSAlertDefaultReturn:
53 [[NSWorkspace sharedWorkspace] openFile:@"/System/Library/PreferencePanes/UniversalAccessPref.prefPane"];
54 while( !(axOn = AXAPIEnabled()) )
55 ; // Wait for accessibility to be turned on.
56 break;
57
58 case NSAlertAlternateReturn:
59 [NSApp terminate: self];
60 return NO;
61 break;
62
63 case NSAlertOtherReturn: // just continue
64 default:
65 break;
66 }
67 }
68
69 return axOn;
70 }
71
72 +(UKUIElement*) systemWideUIElement
73 {
74 static UKUIElement* sysWideUIElement = nil;
75
76 if( !sysWideUIElement )
77 sysWideUIElement = [[UKUIElement alloc] initWithUIElementRef: AXUIElementCreateSystemWide()];
78
79 return sysWideUIElement;
80 }
81
82
83 +(UKUIElement*) applicationUIElement
84 {
85 return [self uiElementForApplicationWithPID: getpid()];
86 }
87
88
89 +(UKUIElement*) uiElementForApplicationWithPID: (pid_t)thepid
90 {
91 UKUIElement* appUIElement = nil;
92 AXUIElementRef ref = AXUIElementCreateApplication( thepid );
93
94 appUIElement = [[[UKUIElement alloc] initWithUIElementRef: ref] autorelease];
95
96 CFRelease( ref );
97
98 return appUIElement;
99 }
100
101
102 -(id) initWithUIElementRef: (AXUIElementRef)ref
103 {
104 if( (self = [super init]) )
105 {
106 theUIElement = ref;
107 CFRetain( theUIElement );
108 }
109
110 return self;
111 }
112
113 -(void) dealloc
114 {
115 CFRelease( theUIElement );
116
117 [super dealloc];
118 }
119
120
121 -(NSArray*) allKeys
122 {
123 NSArray* arr = nil;
124 AXError err = kAXErrorSuccess;
125
126 err = AXUIElementCopyAttributeNames( theUIElement, (CFArrayRef*) &arr );
127 if( err != kAXErrorSuccess )
128 {
129 UKLog( @"allKeys AXUIElementCopyAttributeNames: MacOS Error ID = %d", err );
130 return nil;
131 }
132
133 return [arr autorelease];
134 }
135
136
137 -(pid_t) owningAppPID
138 {
139 pid_t pid = 0;
140
141 if( kAXErrorSuccess != AXUIElementGetPid( theUIElement, &pid ) )
142 return 0;
143 else
144 return pid;
145 }
146
147
148 -(id) objectForKey: (NSString*)keyStr
149 {
150 AXError err = kAXErrorSuccess;
151 id val = nil;
152
153 err = AXUIElementCopyAttributeValue( theUIElement, (CFStringRef) keyStr, (CFTypeRef*) &val );
154 if( err != kAXErrorSuccess )
155 {
156 UKLog( @"objectForKey AXUIElementCopyAttributeValue: MacOS Error ID = %d", err );
157 return nil;
158 }
159
160 return [val autorelease];
161 }
162
163
164 -(NSArray*) arrayForKey: (NSString*)keyStr
165 {
166 AXError err = kAXErrorSuccess;
167 NSArray* val = nil;
168
169 err = AXUIElementCopyAttributeValues( theUIElement, (CFStringRef) keyStr, 0, 1000000, (CFArrayRef*) &val );
170 if( err != kAXErrorSuccess )
171 {
172 UKLog( @"objectForKey AXUIElementCopyAttributeValues: MacOS Error ID = %d", err );
173 return nil;
174 }
175
176 return [val autorelease];
177 }
178
179
180 -(void) setObject: (id)val forKey: (NSString*)keyStr
181 {
182 AXError err = kAXErrorSuccess;
183
184 err = AXUIElementSetAttributeValue( theUIElement, (CFStringRef) keyStr, (CFTypeRef)val );
185
186 if( err != kAXErrorSuccess )
187 UKLog( @"setObject:forKey: AXUIElementSetAttributeValue: MacOS Error ID = %d", err );
188 }
189
190
191 -(AXUIElementRef) uiElementRef
192 {
193 return theUIElement;
194 }
195
196
197 -(NSString*) description
198 {
199 return [(NSString*) CFCopyDescription(theUIElement) autorelease];
200 }
201
202
203 @end
204
205
206 @implementation UKUINotificationCenter
207
208 -(id) initWithPID: (pid_t)thepid
209 {
210 if( (self = [super init]) )
211 {
212 if( kAXErrorSuccess != AXObserverCreate( thepid, UKUIObserverCallback, &theObserver ) )
213 {
214 [self autorelease];
215 return nil;
216 }
217 }
218
219 return self;
220 }
221
222
223 -(void) addObserver: (id)object selector: (SEL)sel name: (NSString*)nm object: (UKUIElement*)ele
224 {
225 AXObserverAddNotification( theObserver, [ele uiElementRef], (CFStringRef) nm, object );
226 }
227
228
229 -(void) removeObserver: (id)object name: (NSString*)nm object: (UKUIElement*)ele
230 {
231 AXObserverRemoveNotification( theObserver, [ele uiElementRef], (CFStringRef) nm );
232 }
233
234 -(void) removeObserver: (id)object
235 {
236
237 }
238
239 @end
240
241 void UKUIObserverCallback( AXObserverRef observer, AXUIElementRef element, CFStringRef notification, void *refcon )
242 {
243 id obj = (id)refcon;
244
245 [obj accessibilityNotification];
246 }
Something went wrong with that request. Please try again.