blob: 3e4e22393c04906f8983ec2878dea71d2d4cd0f0 [file] [log] [blame]
Jack Jansenb7276cd2002-07-31 13:15:59 +00001/*
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
14/* I assume I could pick these up from somewhere, but where... */
15#define CREATOR 'trmx'
16
17#define ACTIVATE_CMD 'misc'
18#define ACTIVATE_SUITE 'actv'
19
20#define DOSCRIPT_CMD 'dosc'
21#define DOSCRIPT_SUITE 'core'
22#define WITHCOMMAND 'cmnd'
23
24/* ... and there's probably also a better way to do this... */
25#define START_TERMINAL "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal &"
26
27extern int
28doscript(const char *command)
29{
30 OSErr err;
31 AppleEvent theAEvent, theReply;
32 AEAddressDesc terminalAddress;
33 AEDesc commandDesc;
34 OSType terminalCreator = CREATOR;
35
36 /* set up locals */
37 AECreateDesc(typeNull, NULL, 0, &theAEvent);
38 AECreateDesc(typeNull, NULL, 0, &terminalAddress);
39 AECreateDesc(typeNull, NULL, 0, &theReply);
40 AECreateDesc(typeNull, NULL, 0, &commandDesc);
41
42 /* create the "activate" event for Terminal */
43 err = AECreateDesc(typeApplSignature, (Ptr) &terminalCreator,
44 sizeof(terminalCreator), &terminalAddress);
45 if (err != noErr) {
46 NSLog(@"doscript: AECreateDesc: error %d\n", err);
47 goto bail;
48 }
49 err = AECreateAppleEvent(ACTIVATE_SUITE, ACTIVATE_CMD,
50 &terminalAddress, kAutoGenerateReturnID,
51 kAnyTransactionID, &theAEvent);
52
53 if (err != noErr) {
54 NSLog(@"doscript: AECreateAppleEvent(activate): error %d\n", err);
55 goto bail;
56 }
57 /* send the event */
58 err = AESend(&theAEvent, &theReply, kAEWaitReply,
59 kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
60 if ( err == -600 ) {
61 int count=10;
62 /* If it failed with "no such process" try to start Terminal */
63 err = system(START_TERMINAL);
64 if ( err ) {
65 NSLog(@"doscript: system(): %s\n", strerror(errno));
66 goto bail;
67 }
68 do {
69 sleep(1);
70 /* send the event again */
71 err = AESend(&theAEvent, &theReply, kAEWaitReply,
72 kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
73 } while (err == -600 && --count > 0);
74 if ( err == -600 )
75 NSLog(@"doscript: Could not activate Terminal\n");
76 }
77 if (err != noErr) {
78 NSLog(@"doscript: AESend(activate): error %d\n", err);
79 goto bail;
80 }
81
82 /* create the "doscript with command" event for Terminal */
83 err = AECreateAppleEvent(DOSCRIPT_SUITE, DOSCRIPT_CMD,
84 &terminalAddress, kAutoGenerateReturnID,
85 kAnyTransactionID, &theAEvent);
86 if (err != noErr) {
87 NSLog(@"doscript: AECreateAppleEvent(doscript): error %d\n", err);
88 goto bail;
89 }
90
91 /* add the command to the apple event */
92 err = AECreateDesc(typeChar, command, strlen(command), &commandDesc);
93 if (err != noErr) {
94 NSLog(@"doscript: AECreateDesc(command): error %d\n", err);
95 goto bail;
96 }
97 err = AEPutParamDesc(&theAEvent, WITHCOMMAND, &commandDesc);
98 if (err != noErr) {
99 NSLog(@"doscript: AEPutParamDesc: error %d\n", err);
100 goto bail;
101 }
102
Just van Rossum85e4c672003-01-04 16:26:26 +0000103 /* send the command event to Terminal.app */
Jack Jansenb7276cd2002-07-31 13:15:59 +0000104 err = AESend(&theAEvent, &theReply, kAEWaitReply,
105 kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
106
107 if (err != noErr) {
108 NSLog(@"doscript: AESend(docommand): error %d\n", err);
109 goto bail;
110 }
111 /* clean up and leave */
112bail:
113 AEDisposeDesc(&commandDesc);
114 AEDisposeDesc(&theAEvent);
115 AEDisposeDesc(&terminalAddress);
116 AEDisposeDesc(&theReply);
117 return err;
Just van Rossum85e4c672003-01-04 16:26:26 +0000118}