[Python-checkins] python/dist/src/Mac/OSX/PythonLauncher doscript.h,NONE,1.1 doscript.m,NONE,1.1 FileSettings.m,1.1,1.2 MyDocument.m,1.1,1.2

jackjansen@users.sourceforge.net jackjansen@users.sourceforge.net
Wed, 31 Jul 2002 06:16:01 -0700


Update of /cvsroot/python/python/dist/src/Mac/OSX/PythonLauncher
In directory usw-pr-cvs1:/tmp/cvs-serv4089

Modified Files:
	FileSettings.m MyDocument.m 
Added Files:
	doscript.h doscript.m 
Log Message:
Implemented starting Python in a terminal window. The implementation isn't
optimal, especially if Terminal wasn't running yet, but it works.


--- NEW FILE: doscript.h ---
/*
 *  doscript.h
 *  PythonLauncher
 *
 *  Created by Jack Jansen on Wed Jul 31 2002.
 *  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
 *
 */

#include <Carbon/Carbon.h>

extern int doscript(const char *command);
--- NEW FILE: doscript.m ---
/*
 *  doscript.c
 *  PythonLauncher
 *
 *  Created by Jack Jansen on Wed Jul 31 2002.
 *  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
 *
 */

#import <Cocoa/Cocoa.h>
#import <ApplicationServices/ApplicationServices.h>
#import "doscript.h"

/* I assume I could pick these up from somewhere, but where... */
#define CREATOR 'trmx'

#define ACTIVATE_CMD 'misc'
#define ACTIVATE_SUITE 'actv'

#define DOSCRIPT_CMD 'dosc'
#define DOSCRIPT_SUITE 'core'
#define WITHCOMMAND 'cmnd'

/* ... and there's probably also a better way to do this... */
#define START_TERMINAL "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal &"

extern int 
doscript(const char *command)
{
    OSErr err;
    AppleEvent theAEvent, theReply;
    AEAddressDesc terminalAddress;
    AEDesc commandDesc;
    OSType terminalCreator = CREATOR;
    
    /* set up locals  */
    AECreateDesc(typeNull, NULL, 0, &theAEvent);
    AECreateDesc(typeNull, NULL, 0, &terminalAddress);
    AECreateDesc(typeNull, NULL, 0, &theReply);
    AECreateDesc(typeNull, NULL, 0, &commandDesc);
    
    /* create the "activate" event for Terminal */
    err = AECreateDesc(typeApplSignature, (Ptr) &terminalCreator,
            sizeof(terminalCreator), &terminalAddress);
    if (err != noErr) {
        NSLog(@"doscript: AECreateDesc: error %d\n", err);
        goto bail;
    }
    err = AECreateAppleEvent(ACTIVATE_SUITE, ACTIVATE_CMD,
            &terminalAddress, kAutoGenerateReturnID,
            kAnyTransactionID, &theAEvent);
    
    if (err != noErr) {
        NSLog(@"doscript: AECreateAppleEvent(activate): error %d\n", err);
        goto bail;
    }
    /* send the event  */
    err = AESend(&theAEvent, &theReply, kAEWaitReply,
            kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
    if ( err == -600 ) {
        int count=10;
        /* If it failed with "no such process" try to start Terminal */
        err = system(START_TERMINAL);
        if ( err ) {
            NSLog(@"doscript: system(): %s\n", strerror(errno));
            goto bail;
        }
        do {
            sleep(1);
            /* send the event again */
            err = AESend(&theAEvent, &theReply, kAEWaitReply,
                    kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
        } while (err == -600 && --count > 0);
        if ( err == -600 )
            NSLog(@"doscript: Could not activate Terminal\n");
    }
    if (err != noErr) {
        NSLog(@"doscript: AESend(activate): error %d\n", err);
        goto bail;
    }
            
    /* create the "doscript with command" event for Terminal */
    err = AECreateAppleEvent(DOSCRIPT_SUITE, DOSCRIPT_CMD,
            &terminalAddress, kAutoGenerateReturnID,
            kAnyTransactionID, &theAEvent);
    if (err != noErr) {
        NSLog(@"doscript: AECreateAppleEvent(doscript): error %d\n", err);
        goto bail;
    }
    
    /* add the command to the apple event */
    err = AECreateDesc(typeChar, command, strlen(command), &commandDesc);
    if (err != noErr) {
        NSLog(@"doscript: AECreateDesc(command): error %d\n", err);
        goto bail;
    }
    err = AEPutParamDesc(&theAEvent, WITHCOMMAND, &commandDesc);
    if (err != noErr) {
        NSLog(@"doscript: AEPutParamDesc: error %d\n", err);
        goto bail;
    }

    /* send the event to the Finder */
    err = AESend(&theAEvent, &theReply, kAEWaitReply,
            kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
    
    if (err != noErr) {
        NSLog(@"doscript: AESend(docommand): error %d\n", err);
        goto bail;
    }
    /* clean up and leave */
bail:
    AEDisposeDesc(&commandDesc);
    AEDisposeDesc(&theAEvent);
    AEDisposeDesc(&terminalAddress);
    AEDisposeDesc(&theReply);
    return err;
}
Index: FileSettings.m
===================================================================
RCS file: /cvsroot/python/python/dist/src/Mac/OSX/PythonLauncher/FileSettings.m,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** FileSettings.m	29 Jul 2002 21:36:35 -0000	1.1
--- FileSettings.m	31 Jul 2002 13:15:59 -0000	1.2
***************
*** 166,170 ****
          others,
          script,
!         with_terminal? "" : " &"];
  }
  
--- 166,170 ----
          others,
          script,
!         with_terminal? "&& exit" : " &"];
  }
  

Index: MyDocument.m
===================================================================
RCS file: /cvsroot/python/python/dist/src/Mac/OSX/PythonLauncher/MyDocument.m,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** MyDocument.m	29 Jul 2002 21:36:35 -0000	1.1
--- MyDocument.m	31 Jul 2002 13:15:59 -0000	1.2
***************
*** 9,12 ****
--- 9,13 ----
  #import "MyDocument.h"
  #import "MyAppDelegate.h"
+ #import "doscript.h"
  
  @implementation MyDocument
***************
*** 72,81 ****
      int sts;
      
!     if ([settings with_terminal]) {
!         NSLog(@"Terminal not implemented yet\n");
!         return NO;
      }
-     cmdline = [[settings commandLineForScript: script] cString];
-     sts = system(cmdline);
      if (sts) {
          NSLog(@"Exit status: %d\n", sts);
--- 73,82 ----
      int sts;
      
!      cmdline = [[settings commandLineForScript: script] cString];
!    if ([settings with_terminal]) {
!         sts = doscript(cmdline);
!     } else {
!         sts = system(cmdline);
      }
      if (sts) {
          NSLog(@"Exit status: %d\n", sts);