Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
AppleScript.m
Go to the documentation of this file.
1#import <Foundation/Foundation.h>
2
3typedef struct {
4 int success;
5 char* output;
6 char* error;
7 int errorCode;
9
10static char* copyString(NSString* str) {
11 if (!str) return NULL;
12 const char* utf8 = [str UTF8String];
13 if (!utf8) return NULL;
14 size_t len = strlen(utf8) + 1;
15 char* buf = (char*)malloc(len);
16 if (buf) memcpy(buf, utf8, len);
17 return buf;
18}
19
21 AppleScriptResult result = {0, NULL, NULL, 0};
22
23 if (!script) {
24 result.error = copyString(@"NULL script pointer");
25 return result;
26 }
27
28 @autoreleasepool {
29 NSString* nsScript = [NSString stringWithUTF8String:script];
30 if (!nsScript) {
31 result.error = copyString(@"Invalid UTF-8 in script string");
32 return result;
33 }
34
35 NSAppleScript* as = [[NSAppleScript alloc] initWithSource:nsScript];
36 NSDictionary* errorInfo = nil;
37 NSAppleEventDescriptor* descriptor = [as executeAndReturnError:&errorInfo];
38
39 if (descriptor) {
40 result.success = 1;
41 result.output = copyString([descriptor stringValue]);
42 } else {
43 result.success = 0;
44 if (errorInfo) {
45 result.error = copyString(errorInfo[NSAppleScriptErrorMessage]);
46 result.errorCode = [errorInfo[NSAppleScriptErrorNumber] intValue];
47 }
48 }
49 }
50
51 return result;
52}
53
55 if (!r) return;
56 free(r->output); r->output = NULL;
57 free(r->error); r->error = NULL;
58}
void freeAppleScriptResult(AppleScriptResult *r)
Definition AppleScript.m:54
AppleScriptResult executeAppleScript(const char *script)
Definition AppleScript.m:20
static char * copyString(NSString *str)
Definition AppleScript.m:10
#define error(msg)
Definition nativeerror.h:8