1/*
2 *  doscript.c
3 *  PythonLauncher
4 *
5 *  Created by Jack Jansen on Wed Jul 31 2002.
6 *  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
7 *
8 */
9
10#import <Cocoa/Cocoa.h>
11#import <ApplicationServices/ApplicationServices.h>
12#import "doscript.h"
13
14extern int
15doscript(const char *command)
16{
17    char *bundleID = "com.apple.Terminal";
18    AppleEvent evt, res;
19    AEDesc desc;
20    OSStatus err;
21
22    [[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Utilities/Terminal.app/"];
23
24    // Build event
25    err = AEBuildAppleEvent(kAECoreSuite, kAEDoScript,
26                             typeApplicationBundleID,
27                             bundleID, strlen(bundleID),
28                             kAutoGenerateReturnID,
29                             kAnyTransactionID,
30                             &evt, NULL,
31                             "'----':utf8(@)", strlen(command),
32                             command);
33    if (err) {
34        NSLog(@"AEBuildAppleEvent failed: %ld\n", (long)err);
35        return err;
36    }
37
38    // Send event and check for any Apple Event Manager errors
39    err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
40    AEDisposeDesc(&evt);
41    if (err) {
42        NSLog(@"AESendMessage failed: %ld\n", (long)err);
43        return err;
44    }
45    // Check for any application errors
46    err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &desc);
47    AEDisposeDesc(&res);
48    if (!err) {
49        AEGetDescData(&desc, &err, sizeof(err));
50        NSLog(@"Terminal returned an error: %ld", (long)err);
51        AEDisposeDesc(&desc);
52    } else if (err == errAEDescNotFound) {
53        err = noErr;
54    } else {
55        NSLog(@"AEGetPArmDesc returned an error: %ld", (long)err);
56    }
57
58    return err;
59}
60