~ubuntu-branches/ubuntu/oneiric/python2.5/oneiric

« back to all changes in this revision

Viewing changes to Mac/PythonLauncher/doscript.m

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-12-21 08:57:49 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20081221085749-bijjr25h8na5jdsu
Tags: 2.5.3-0ubuntu1
* New upstream version.
* Regenerate the included documentation.
* Add an option --install-layout=deb, which is ignored for 2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#import <ApplicationServices/ApplicationServices.h>
12
12
#import "doscript.h"
13
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
 
 
27
14
extern int 
28
15
doscript(const char *command)
29
16
{
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
 
 
103
 
    /* send the command event to Terminal.app */
104
 
    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 */
112
 
bail:
113
 
    AEDisposeDesc(&commandDesc);
114
 
    AEDisposeDesc(&theAEvent);
115
 
    AEDisposeDesc(&terminalAddress);
116
 
    AEDisposeDesc(&theReply);
117
 
    return err;
 
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: %d\n", 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: %d\n", 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: %d", err);
 
51
                AEDisposeDesc(&desc);
 
52
        } else if (err == errAEDescNotFound) {
 
53
                err = noErr;
 
54
        } else {
 
55
                NSLog(@"AEGetPArmDesc returned an error: %d", err);
 
56
        }
 
57
 
 
58
        return err;
118
59
}