3 Created by Evan Schoenberg on 10/31/2004
6 #import <Foundation/Foundation.h>
7 #import <Carbon/Carbon.h>
10 @brief Shell tool to run an applescript, optionally with a function name and arguments
12 * ApplescriptRunner takes one or more arguments.
13 * The first argument should be the full path to an Applescript script file.
14 * The second argument may be the name of a function.
15 * Any additional arguments are passed to the called function in that file.
17 * Minimal error checking is performed.
19 int main (int argc, const char *argv[])
21 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
22 NSProcessInfo *processInfo;
23 NSAppleScript *appleScript;
24 NSAppleEventDescriptor *thisApplication, *containerEvent;
25 NSString *functionName = nil, *resultString = nil;
26 NSArray *processArguments, *scriptArgumentArray = nil;
28 unsigned processArgumentsCount;
30 processInfo = [NSProcessInfo processInfo];
31 processArguments = [processInfo arguments];
32 processArgumentsCount = [processArguments count];
34 //The first argument is always the path to this program. The second should be a path to the script
35 pathURL = [NSURL fileURLWithPath:[processArguments objectAtIndex:1]];
37 //Any additonal arguments should be the function to be called and arguments for it
38 if (processArgumentsCount > 2) {
39 functionName = [processArguments objectAtIndex:2];
41 if (processArgumentsCount > 3) {
42 scriptArgumentArray = [processArguments subarrayWithRange:NSMakeRange(3, processArgumentsCount-3)];
46 appleScript = [[NSAppleScript alloc] initWithContentsOfURL:pathURL
51 /* If we have a functionName (and potentially arguments), we build
52 * an NSAppleEvent to execute the script. */
54 //Get a descriptor for ourself
55 int pid = [processInfo processIdentifier];
56 thisApplication = [NSAppleEventDescriptor descriptorWithDescriptorType:typeKernelProcessID
60 //Create the container event
61 containerEvent = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite
62 eventID:kASSubroutineEvent
63 targetDescriptor:thisApplication
64 returnID:kAutoGenerateReturnID
65 transactionID:kAnyTransactionID];
67 //Set the target function
68 [containerEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:functionName]
69 forKeyword:keyASSubroutineName];
71 //Pass arguments - arguments is expecting an NSArray with only NSString objects
72 if ([scriptArgumentArray count]) {
73 NSAppleEventDescriptor *arguments = [[NSAppleEventDescriptor alloc] initListDescriptor];
74 NSEnumerator *enumerator;
77 enumerator = [scriptArgumentArray objectEnumerator];
78 while ((object = [enumerator nextObject])) {
79 [arguments insertDescriptor:[NSAppleEventDescriptor descriptorWithString:object]
80 atIndex:([arguments numberOfItems] + 1)]; //This +1 seems wrong... but it's not
83 [containerEvent setParamDescriptor:arguments forKeyword:keyDirectObject];
88 resultString = [[appleScript executeAppleEvent:containerEvent error:NULL] stringValue];
91 resultString = [[appleScript executeAndReturnError:NULL] stringValue];
95 //Print the resulting string to standard output
97 printf("%s", [resultString UTF8String]);
100 [appleScript release];
103 return !resultString;