forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSAppleScript+ThreadSafe.h
87 lines (77 loc) · 3.3 KB
/
NSAppleScript+ThreadSafe.h
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
#import <Cocoa/Cocoa.h>
@interface NSAppleScript (ThreadSafe)
/*!
@brief Executes an AppleScript source on the main thread,
returning after the execution is complete.
@param source
@param error_p If an error occurs, and if error_p is not
nil, a descriptive error object will be assigned to *error_p.
@result The result returned by the script, or nil if an error
occurred. If the script returned null, result will be an
NSAppleEventDescriptor object wrapping null.
*/
+ (NSAppleEventDescriptor*)threadSafelyExecuteSource:(NSString*)source
error_p:(NSError**)error_p ;
@end
#if 0
// Here is some code that I found, but ended up not using, to
// execute a script with parameters passed. It comes from here:
// http://www.cocoadev.com/index.pl?CallAppleScriptFunction
// Author is Benoît Marchal
// http://www.marchal.com/en/
// http://www.cocoadev.com/index.pl?BenoitMarchal
First, the demo AppleScript resource:
on show_message(user_message)
tell application "Finder"
display dialog user_message
end tell
end show_message
// Now, the Cocoa code
NSDictionary *errors = [NSDictionary dictionary];
NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"scpt"];
if(path)
{
NSURL *url = [NSURL fileURLWithPath:path];
if(url)
{
// load the script from a resource
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
if(appleScript)
{
// create the first (and in this case only) parameter
// note we can't pass an NSString (or any other object
// for that matter) to AppleScript directly,
// must convert to NSAppleEventDescriptor first
NSAppleEventDescriptor *firstParameter = [NSAppleEventDescriptor descriptorWithString:@"my message"];
// create and populate the list of parameters
// note that the array starts at index 1
NSAppleEventDescriptor *parameters = [NSAppleEventDescriptor listDescriptor];
[parameters insertDescriptor:firstParameter atIndex:1];
// create the AppleEvent target
ProcessSerialNumber psn = { 0, kCurrentProcess };
NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber
bytes:&psn
length:sizeof(ProcessSerialNumber)];
// create an NSAppleEventDescriptor with the method name
// note that the name must be lowercase (even if it is uppercase in AppleScript)
NSAppleEventDescriptor *handler = [NSAppleEventDescriptor descriptorWithString:[@"show_message" lowercaseString]];
// last but not least, create the event for an AppleScript subroutine
// set the method name and the list of parameters
NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite
eventID:kASSubroutineEvent
targetDescriptor:target
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
[event setParamDescriptor:handler forKeyword:keyASSubroutineName];
[event setParamDescriptor:parameters forKeyword:keyDirectObject];
// at last, call the event in AppleScript
if(![appleScript executeAppleEvent:event error:&errors]);
ReportAppleScriptErrors(errors);
[appleScript release];
}
else {
ReportAppleScriptErrors(errors);
}
}
}
#endif