Don't need the English form of these
[adiumx.git] / Source / ApplescriptRunner.m
blob6de0321bc7a7182b34bdd0607cb6bad806e10d12
1 /*
2  ApplescriptRunner.m
3  Created by Evan Schoenberg on 10/31/2004
4 */
6 #import <Foundation/Foundation.h>
7 #import <Carbon/Carbon.h>
9 /*
10  @brief Shell tool to run an applescript, optionally with a function name and arguments
11  *
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.
16  *
17  * Minimal error checking is performed.
18  */
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;
27         NSURL                                   *pathURL;
28         unsigned                                processArgumentsCount;
30         processInfo = [NSProcessInfo processInfo];
31         processArguments = [processInfo arguments];
32         processArgumentsCount = [processArguments count];
33          
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)];
43                 }
44         }
46         appleScript = [[NSAppleScript alloc] initWithContentsOfURL:pathURL
47                                                                                                                  error:NULL];
49         if (appleScript) {
50                 if (functionName) {
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
57                                                                                                                                                          bytes:&pid
58                                                                                                                                                         length:sizeof(pid)];
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;
75                                 NSString                                *object;
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
81                                 }
83                                 [containerEvent setParamDescriptor:arguments forKeyword:keyDirectObject];
84                                 [arguments release];
85                         }
87                         //Execute the event
88                         resultString = [[appleScript executeAppleEvent:containerEvent error:NULL] stringValue];
90                 } else {
91                         resultString = [[appleScript executeAndReturnError:NULL] stringValue];
92                 }
93         }
95         //Print the resulting string to standard output
96         if (resultString) {
97                 printf("%s", [resultString UTF8String]);
98         }
100         [appleScript release];
101         [pool release];
103         return !resultString;