Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 355 lines (231 sloc) 9.546 kb
ee99867 @ccgus oh geeze
authored
1 //
2 // JSTExtras.m
3 // jsenabler
4 //
5 // Created by August Mueller on 1/15/09.
6 // Copyright 2009 Flying Meat Inc. All rights reserved.
7 //
8
9 #import "JSTExtras.h"
1a4ff63 Added support for some sort of JSTalk envirnoment vars, which you can st...
August Mueller authored
10 #import "JSTalk.h"
ee99867 @ccgus oh geeze
authored
11 #import <ScriptingBridge/ScriptingBridge.h>
12
1a4ff63 Added support for some sort of JSTalk envirnoment vars, which you can st...
August Mueller authored
13 @implementation JSTalk (JSTExtras)
14
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
15 - (void)exit:(int)termCode {
1a4ff63 Added support for some sort of JSTalk envirnoment vars, which you can st...
August Mueller authored
16 exit(termCode);
17 }
18
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
19 - (void)modifiers:(NSString*)using down:(BOOL)pressDown {
20
abbae48 @ccgus Using a newer api for this, to fix deprecation warnings.
authored
21 // THIS IS TOTOALLY BROKEN, BUT AT LEAST IT DOESN'T USE DEPREICATED APIS ANYMORE.
22
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
23 // we're doing it this way, since for some reason, it doesn't always
24 // work correct when using the "using command & option down" stuff
25 // for applescript. I DON'T KNOW WHY IT JUST DOESN'T.
26
27 BOOL option = [using rangeOfString:@"option"].location != NSNotFound;
28 BOOL command = [using rangeOfString:@"command"].location != NSNotFound;
29 BOOL control = [using rangeOfString:@"control"].location != NSNotFound;
30 BOOL shift = [using rangeOfString:@"shift"].location != NSNotFound;
31
32 if (command) {
abbae48 @ccgus Using a newer api for this, to fix deprecation warnings.
authored
33 debug(@"sending command: %d", pressDown);
34 CGEventRef event = CGEventCreateKeyboardEvent(nil, (CGKeyCode)55, pressDown);
35 CGEventPost(kCGHIDEventTap, event);
36 CFRelease(event);
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
37 }
38 if (option) {
abbae48 @ccgus Using a newer api for this, to fix deprecation warnings.
authored
39 CGEventRef event = CGEventCreateKeyboardEvent(nil, (CGKeyCode)58, pressDown);
40 CGEventPost(kCGHIDEventTap, event);
41 CFRelease(event);
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
42 }
43
44 if (control) {
abbae48 @ccgus Using a newer api for this, to fix deprecation warnings.
authored
45 CGEventRef event = CGEventCreateKeyboardEvent(nil, (CGKeyCode)59, pressDown);
46 CGEventPost(kCGHIDEventTap, event);
47 CFRelease(event);
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
48 }
49 if (shift) {
abbae48 @ccgus Using a newer api for this, to fix deprecation warnings.
authored
50 CGEventRef event = CGEventCreateKeyboardEvent(nil, (CGKeyCode)56, pressDown);
51 CGEventPost(kCGHIDEventTap, event);
52 CFRelease(event);
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
53 }
54 }
55
56
57 - (void)keystroke:(NSString*)keys using:(NSString*)using {
58 keys = [keys stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
59
60 NSString *appleScriptString = [NSString stringWithFormat:@"tell application \"System Events\"\nkeystroke \"%@\"\nend tell", keys];
61
62 NSAppleScript *as = [[[NSAppleScript alloc] initWithSource:appleScriptString] autorelease];
63
64 [self modifiers:using down:YES];
65
66 NSDictionary *err;
67 if (![as executeAndReturnError:&err]) {
68 NSLog(@"Error: %@", err);
69 }
70
71 [self modifiers:using down:NO];
72
73 }
74
75 - (void)keystroke:(NSString*)keys {
76 [self keystroke:keys using:@""];
77 }
78
79 - (void)keyCode:(NSString*)keys using:(NSString*)using {
80
81 NSString *appleScriptString = [NSString stringWithFormat:@"tell application \"System Events\"\nkey code %@\nend tell", keys];
82
83 NSAppleScript *as = [[[NSAppleScript alloc] initWithSource:appleScriptString] autorelease];
84
85 [self modifiers:using down:YES];
86
87 NSDictionary *err;
88 if (![as executeAndReturnError:&err]) {
89 NSLog(@"Error: %@", err);
90 }
91
92 [self modifiers:using down:NO];
93 }
94
95 - (void)keyCode:(NSString*)keys {
96 [self keyCode:keys using:@""];
97 }
98
99 - (void)sleep:(CGFloat)s {
100 sleep(s);
101 }
102
103
1a4ff63 Added support for some sort of JSTalk envirnoment vars, which you can st...
August Mueller authored
104 @end
105
ee99867 @ccgus oh geeze
authored
106 @implementation NSApplication (JSTExtras)
107
108 - (id)open:(NSString*)pathToFile {
109
110 NSError *err = 0x00;
111
112 NSURL *url = [NSURL fileURLWithPath:pathToFile];
113
114 id doc = [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:url
115 display:YES
116 error:&err];
117
118 if (err) {
119 NSLog(@"Error: %@", err);
120 return nil;
121 }
122
123 return doc;
124 }
125
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
126 - (void)activate {
ee99867 @ccgus oh geeze
authored
127 ProcessSerialNumber xpsn = { 0, kCurrentProcess };
128 SetFrontProcess( &xpsn );
129 }
130
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
131 - (NSInteger)displayDialog:(NSString*)msg withTitle:(NSString*) title {
ee99867 @ccgus oh geeze
authored
132
133 NSAlert *alert = [NSAlert alertWithMessageText:title defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:msg];
134
135 NSInteger button = [alert runModal];
136
137 return button;
138 }
139
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
140 - (NSInteger)displayDialog:(NSString*)msg {
ee99867 @ccgus oh geeze
authored
141
142 NSString *title = [[[NSBundle mainBundle] infoDictionary] objectForKey:(id)kCFBundleNameKey];
143
144 if (!title) {
145 title = @"Unknown Application";
146 }
147
148 return [self displayDialog:msg withTitle:title];
149 }
150
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
151 - (id)sharedDocumentController {
ee99867 @ccgus oh geeze
authored
152 return [NSDocumentController sharedDocumentController];
153 }
154
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
155 - (id)standardUserDefaults {
ee99867 @ccgus oh geeze
authored
156 return [NSUserDefaults standardUserDefaults];
157 }
158
159 @end
160
161
162 @implementation NSDocument (JSTExtras)
163
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
164 - (id)dataOfType:(NSString*)type {
ee99867 @ccgus oh geeze
authored
165
166 NSError *err = 0x00;
167
168 NSData *data = [self dataOfType:type error:&err];
169
170
171 return data;
172
173 }
174
175 @end
176
177
178 @implementation NSData (JSTExtras)
179
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
180 - (BOOL)writeToFile:(NSString*)path {
ee99867 @ccgus oh geeze
authored
181
182 return [self writeToURL:[NSURL fileURLWithPath:path] atomically:YES];
183 }
184
185 @end
186
187 @implementation NSObject (JSTExtras)
188
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
189 - (Class)ojbcClass {
ee99867 @ccgus oh geeze
authored
190 return [self class];
191 }
192
193 @end
194
195
196 @implementation SBApplication (JSTExtras)
197
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
198 + (id)application:(NSString*)appName {
ee99867 @ccgus oh geeze
authored
199
200 NSString *appPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:appName];
201
202 if (!appPath) {
203 NSLog(@"Could not find application '%@'", appName);
204 return nil;
205 }
206
207 NSBundle *appBundle = [NSBundle bundleWithPath:appPath];
208 NSString *bundleId = [appBundle bundleIdentifier];
209
210 return [SBApplication applicationWithBundleIdentifier:bundleId];
211 }
212
213
214 @end
215
216
217
218 @implementation NSString (JSTExtras)
219
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
220 - (NSURL*)fileURL {
ee99867 @ccgus oh geeze
authored
221 return [NSURL fileURLWithPath:self];
222 }
223
224 @end
225
226
e7f6508 @ccgus JSTalk's executeString: now returns an object, which is the result of th...
authored
227 @implementation NSApplication (JSTRandomCrap)
228
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
229 + (NSDictionary*)JSTAXStuff {
e7f6508 @ccgus JSTalk's executeString: now returns an object, which is the result of th...
authored
230
231 NSMutableDictionary *d = [NSMutableDictionary dictionary];
232
233
234 AXUIElementRef uiElement = AXUIElementCreateSystemWide();
235
236 CFTypeRef focusedUIElement = 0x00;
237
238 AXError error = AXUIElementCopyAttributeValue(uiElement, kAXFocusedUIElementAttribute, &focusedUIElement);
239
240 if (focusedUIElement) {
241
242 NSArray* attributeNames;
243 AXUIElementCopyAttributeNames(focusedUIElement, (CFArrayRef *)&attributeNames);
244
245 for (NSString *attName in attributeNames) {
246
247 CFTypeRef attValue;
248
44a9c46 @ccgus Fixed some problems where the ruler view wouldn't show up correctly in 6...
authored
249 AXError lerror = AXUIElementCopyAttributeValue(focusedUIElement, (CFStringRef)attName, &attValue);
250 if (!lerror) {
e7f6508 @ccgus JSTalk's executeString: now returns an object, which is the result of th...
authored
251
252 if ((AXValueGetType(attValue) == kAXValueCGPointType)) {
253 NSPoint p;
254 AXValueGetValue(attValue, kAXValueCGPointType, &p);
255 [d setObject:[NSValue valueWithPoint:p] forKey:attName];
256 }
257 else if ((AXValueGetType(attValue) == kAXValueCGSizeType)) {
258 NSSize s;
259 AXValueGetValue(attValue, kAXValueCGSizeType, &s);
260 [d setObject:[NSValue valueWithSize:s] forKey:attName];
261 }
262 else if ((AXValueGetType(attValue) == kAXValueCGRectType)) {
263 NSRect r;
264 AXValueGetValue(attValue, kAXValueCGRectType, &r);
265 [d setObject:[NSValue valueWithRect:r] forKey:attName];
266 }
267 else if ((AXValueGetType(attValue) == kAXValueCFRangeType)) {
268 NSRange r;
269 AXValueGetValue(attValue, kAXValueCFRangeType, &r);
270 [d setObject:[NSValue valueWithRange:r] forKey:attName];
271 }
272 else {
273 [d setObject:(id)attValue forKey:attName];
274 }
275
276 CFRelease(attValue);
277 }
278
279 }
280
281 if (attributeNames) {
282 CFRelease(attributeNames);
283 }
284
285 CFRelease(focusedUIElement);
286 }
287 else if (error) {
288 NSLog(@"Could not get AXFocusedUIElement");
289 }
290
291
292 CFRelease(uiElement);
293
294 return d;
295
296 }
297
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
298 + (void)JSTAXSetSelectedTextAttributeOnFocusedElement:(NSString*)s {
e7f6508 @ccgus JSTalk's executeString: now returns an object, which is the result of th...
authored
299 AXUIElementRef uiElement = AXUIElementCreateSystemWide();
300
301 CFTypeRef focusedUIElement = 0x00;
302
303 AXError error = AXUIElementCopyAttributeValue(uiElement, kAXFocusedUIElementAttribute, &focusedUIElement);
304
305 if (focusedUIElement) {
306
307 AXUIElementSetAttributeValue(focusedUIElement, kAXSelectedTextAttribute, s);
308
309 CFRelease(focusedUIElement);
310 }
311 else if (error) {
312 NSLog(@"Could not get AXFocusedUIElement");
313 }
314
315 CFRelease(uiElement);
316 }
317
aeb1a40 @ccgus Added some fun "keystroke" commands, which ironicly call into applescrip...
authored
318 + (void)JSTAXSetSelectedTextRangeAttributeOnFocusedElement:(NSRange)range {
e7f6508 @ccgus JSTalk's executeString: now returns an object, which is the result of th...
authored
319 AXUIElementRef uiElement = AXUIElementCreateSystemWide();
320
321 CFTypeRef focusedUIElement = 0x00;
322
323 AXError error = AXUIElementCopyAttributeValue(uiElement, kAXFocusedUIElementAttribute, &focusedUIElement);
324
325 if (focusedUIElement) {
326
327 //sscanf( [[_attributeValueTextField stringValue] cString], "pos=%ld len=%ld", &(range.location), &(range.length) );
328 AXValueRef valueRef = AXValueCreate( kAXValueCFRangeType, (const void *)&range );
329 if (valueRef) {
330 AXError setError = AXUIElementSetAttributeValue(focusedUIElement, kAXSelectedTextRangeAttribute, valueRef );
331
332
333 if (setError) {
334 debug(@"error setting the range (%d)", setError);
335 }
336
337 CFRelease( valueRef );
338 }
339
340
341 CFRelease(focusedUIElement);
342 }
343 else if (error) {
344 NSLog(@"Could not get AXFocusedUIElement");
345 }
346
347 CFRelease(uiElement);
348 }
349
350 @end
351
ee99867 @ccgus oh geeze
authored
352
353
354
Something went wrong with that request. Please try again.