home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / Tools / BBPy / source / BBPy_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-19  |  2.8 KB  |  105 lines  |  [TEXT/CWIE]

  1. /*    BBPython
  2.     A simple menu command to send the contents of a window to the Python interpreter
  3.     
  4.     copyright © 1996 Just van Rossum, Letterror: just@knoware.nl
  5.     
  6.     All Rights Reserved
  7. */
  8.  
  9. #include "BBPy.h"
  10.  
  11. OSErr SendTextAsAE(ExternalCallbackBlock *callbacks, Ptr theText, long theSize, Str255 windowTitle)
  12. {
  13.     OSErr            err;
  14.     AEDesc        theEvent;
  15.     AEAddressDesc    theTarget;
  16.     AppleEvent    theReply;
  17.     AEDesc        theTextDesc;
  18.     AEDesc        theNameDesc;
  19.     OSType        pythonSig = 'Pyth';
  20.     FSSpec        docSpec;
  21.     short            itemHit;
  22.     long            time;
  23.     EventRecord    theDummyEvent;
  24.     
  25.     /* initialize AE descriptor for python's signature */
  26.     err = AECreateDesc (typeApplSignature, &pythonSig, sizeof(OSType), &theTarget);
  27.     if(err != noErr) return err;
  28.     
  29.     /* initialize AE descriptor for the title of our window */
  30.     err = AECreateDesc (typeChar, &windowTitle[1], windowTitle[0], &theNameDesc);
  31.     if(err != noErr) return err;
  32.     
  33.     /* initialize AE descriptor for the content of our window */
  34.     err = AECreateDesc ('TEXT', theText, theSize, &theTextDesc);
  35.     if(err != noErr) return err;
  36.     
  37.     /* initialize AppleEvent */
  38.     err = AECreateAppleEvent ('pyth', 'EXEC', &theTarget, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  39.     if(err != noErr) return err;
  40.     
  41.     /* add the content of our window to the AppleEvent */
  42.     err = AEPutParamDesc (&theEvent, keyDirectObject, &theTextDesc);
  43.     if(err != noErr) return err;
  44.     
  45.     /* add the title of our window to the AppleEvent */
  46.     err = AEPutParamDesc (&theEvent, 'NAME', &theNameDesc);
  47.     if(err != noErr) return err;
  48.     
  49.     /* send the AppleEvent */
  50.     err = AESend (&theEvent, &theReply, kAEWaitReply, kAEHighPriority, kNoTimeOut, NULL, NULL);
  51.     if(err == connectionInvalid) {
  52.         // launch PythonSlave.py
  53.         itemHit = Alert(128, NULL);
  54.         if(itemHit == 2)  return noErr;    /* user cancelled */
  55.         
  56.         if( ! GetPythonSlaveSpec(&docSpec) )
  57.             return noErr;        /* user cancelled */
  58.         
  59.         err = LaunchPythonSlave(&docSpec);
  60.         if(err != noErr) return err;
  61.     } else if(err != noErr) 
  62.         return err;
  63.     
  64.     /* clean up */
  65.     err = AEDisposeDesc (&theTarget);
  66.     if(err != noErr) return err;
  67.     
  68.     err = AEDisposeDesc (&theNameDesc);
  69.     if(err != noErr) return err;
  70.     
  71.     err = AEDisposeDesc (&theTextDesc);
  72.     if(err != noErr) return err;
  73.     
  74.     err = AEDisposeDesc (&theEvent);
  75.     if(err != noErr) return err;
  76.     
  77.     err = AEDisposeDesc (&theReply);
  78.     if(err != noErr) return err;
  79.     
  80.     /* everything is cool */
  81.     return noErr;
  82. }
  83.  
  84. pascal void main(ExternalCallbackBlock *callbacks, WindowPtr theWindow)
  85. {
  86.     long         oldA4;
  87.     OSErr        err;
  88.     Handle    windowContents;
  89.     Str255    windowTitle;
  90.     
  91.     //RememberA0(); /* Can't find header file for this. Seems to work anyway. */
  92.     
  93.     oldA4 = SetUpA4();    
  94.  
  95.     GetWTitle(theWindow, windowTitle);
  96.     windowContents = callbacks->GetWindowContents(theWindow);
  97.     
  98.     HLock(windowContents);
  99.     err = SendTextAsAE(callbacks, *windowContents, GetHandleSize(windowContents), windowTitle);
  100.     if(err != noErr) callbacks->ReportOSError(err);
  101.     HUnlock(windowContents);
  102.     
  103.     RestoreA4(oldA4);
  104. }
  105.