-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSAppleScript+HandlerCalls.m
51 lines (34 loc) · 1.89 KB
/
NSAppleScript+HandlerCalls.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
/*
NSAppleScript+HandlerCalls.m
ASHandlerTest
by Buzz Andersen
More information at: http://www.scifihifi.com/weblog/mac/Cocoa-AppleEvent-Handlers.html
This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit
http://creativecommons.org/licenses/by/1.0/
or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,
California 94305, USA.
*/
#import "NSAppleScript+HandlerCalls.h"
@implementation NSAppleScript (HandlerCalls)
- (NSAppleEventDescriptor *) callHandler: (NSString *) handler withArguments: (NSAppleEventDescriptor *) arguments errorInfo: (NSDictionary **) errorInfo {
NSAppleEventDescriptor* event;
NSAppleEventDescriptor* targetAddress;
NSAppleEventDescriptor* subroutineDescriptor;
NSAppleEventDescriptor* result;
/* This will be a self-targeted AppleEvent, so we need to identify ourselves using our process id */
int pid = [[NSProcessInfo processInfo] processIdentifier];
targetAddress = [[NSAppleEventDescriptor alloc] initWithDescriptorType: typeKernelProcessID bytes: &pid length: sizeof(pid)];
/* Set up our root AppleEvent descriptor: a subroutine call (psbr) */
event = [[NSAppleEventDescriptor alloc] initWithEventClass: 'ascr' eventID: 'psbr' targetDescriptor: targetAddress returnID: kAutoGenerateReturnID transactionID: kAnyTransactionID];
/* Set up an AppleEvent descriptor containing the subroutine (handler) name */
subroutineDescriptor = [NSAppleEventDescriptor descriptorWithString: handler];
[event setParamDescriptor: subroutineDescriptor forKeyword: 'snam'];
/* Add the provided arguments to the handler call */
[event setParamDescriptor: arguments forKeyword: keyDirectObject];
/* Execute the handler */
result = [self executeAppleEvent: event error: errorInfo];
[targetAddress release];
[event release];
return result;
}
@end