home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / mac / hypercar / 3186 < prev    next >
Encoding:
Text File  |  1992-08-21  |  8.4 KB  |  266 lines

  1. Newsgroups: comp.sys.mac.hypercard
  2. Path: sparky!uunet!stanford.edu!kronos.arc.nasa.gov!joshr
  3. From: joshr@kronos.arc.nasa.gov (Joshua Rabinowitz-Summer-91)
  4. Subject: Re: Sending AEs to hypercard
  5. Message-ID: <1992Aug22.001928.27498@kronos.arc.nasa.gov>
  6. Sender: usenet@kronos.arc.nasa.gov (Will Edgington, wedgingt@ptolemy.arc.nasa.gov)
  7. Nntp-Posting-Host: kronos-arclan.arc.nasa.gov
  8. Organization: NASA/ARC Information Sciences Division
  9. References: <EeYJUtS00iUx04XlJV@andrew.cmu.edu>
  10. Date: Sat, 22 Aug 1992 00:19:28 GMT
  11. Lines: 253
  12.  
  13. In article <EeYJUtS00iUx04XlJV@andrew.cmu.edu> "George J. Baxter" <gb2a+@andrew.cmu.edu> writes:
  14. >Gurus:
  15. >    I want to send a string to Hypercard.  I'm using hypercard as a
  16. >front end and I want to send back an error message if I get one.  I
  17. >don't want Hypercard to wait around in any XCMD while I'm looking for my
  18. >error string since there might not be one, or it might be a long time
  19. >coming.  So I want to use Hypercard's built in AE handlers.  I know that
  20. >2.1 supports appleEvents, but how specifically do I address the AE
  21. >handler in hypercard?  The little New Features Guide gives me some
  22. >hints. For example, I could send the appleEvent under class 'misc',  and
  23. >ID 'dosc' with my string being "put <my error string here>"
  24. >or something.  The question I have is where do I put this error string? 
  25. >The 'dosc' ID is described as "interpret the return-delimited list
  26. >accompanying this Apple event as a script and execute it."  
  27. >    Hypercard provides the command "request AppleEvent data", which I
  28. >guess is where the strings in question reside, but again, in my code,
  29. >how do I hand it to Hypercard?  
  30. >    Thanks!
  31. >        -George Baxter
  32. >-----------------------
  33. >gb2a@andrew.cmu.edu            baxter@a.cfr.cmu.edu
  34.  
  35. At the risk of giving away some industry trade secrets,
  36. or just throuwing away my work, I'll GIVE you guys this completely
  37. working code.  It sends a string to hypercard
  38. to be interpreted as a script.  I never got as far as getting hypercard
  39. to return a message like OK or ERROR,though (or maybe
  40. I check for it wrong -- I dont know).  If someone would like
  41. to outline how this would be added to this code, I'd be greatful (but
  42. fair's fair) -- here ya go, netters, the fruit of a bunch of labor.
  43.  
  44. THis code WORKS.
  45.  
  46. written under THINK C5.02
  47. --------- HEADER __________
  48.  
  49. #include <AppleEvents.h>
  50. #include <stdio.h>  // for printf()
  51. #include <string.h>
  52.  
  53.  
  54. #define kHypercardSignature 'WILD'
  55. #define kHypercardMessageEventClass 'misc'
  56. #define kHypercardMessageEventID    'dosc'
  57.  
  58.  
  59. /* PROTOTYPES */
  60. pascal OSErr myEventHandler(AppleEvent *event, AppleEvent *reply, long ref);
  61. Boolean SendToHypercard(char *messageToSend);
  62. pascal Boolean myIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle mouseRgn);
  63. pascal Boolean HypercardFilter(LocationNamePtr locationName, PortInfoPtr thePortInfo);
  64. Boolean FindHypercardProcessNumber(ProcessSerialNumber *process);
  65. Boolean FindAProcess(OSType signature, ProcessSerialNumber *process, 
  66.                    ProcessInfoRec *infoRec, FSSpecPtr aFSSPec);   
  67. /* from InsideMac VI, p. 29-11, "process manager" */
  68.  
  69.   ---------SOURCE---------
  70.  
  71. nclude "SendToHyp.h"
  72. #include <Exceptions.h>
  73. #include "Constants.h"
  74. #include <TBUtilities.h>
  75.  
  76.  
  77. Boolean SendToHypercard(char *messageToSend) 
  78. {
  79.   // from Programming for SYS7, p. 145
  80.    // pseudocode:
  81.    
  82.    // determine target address using PPCBrowser() 
  83.    // or GetNextProcess() and GetProcessInformation()
  84.    
  85.    // create address descriptor from above info
  86.    
  87.    // create apple event with AECreateAppleEvent() and pass it address descriptor
  88.    //  use AEManager to add descriptors with AEPutParamPtr or AEPutParamDesc
  89.    
  90.    // call AESend
  91.    // check for errors
  92.    // dispose of copies of descriptor records
  93.    // process the AEReply
  94.    
  95.    
  96.    
  97.    AEAddressDesc     theAddressDesc;
  98.    AEDesc        theResultDescriptor;
  99.    AppleEvent     theAE, theReply;
  100.    OSErr         myError, sendError;
  101.    Str255         myPrompt = "\pPlease Locate Hypercard:";
  102.    Str255         myListTitle = "\pHypercard Hosts:";
  103.    TargetID     myTarget;
  104.    PortInfoRec    myPortInfo;
  105.    DescType     actualType, theDescriptorType;
  106.    long         eventError, actualSize, theResultSize;
  107.    ProcessSerialNumber process;
  108.    
  109.    // printf("%s\n", messageToSend);
  110.    /*****
  111.    myError = PPCBrowser(myPrompt, myListTitle, FALSE, &(myTarget.location), 
  112.       &myPortInfo, (PPCFilterProcPtr)HypercardFilter, 0L);
  113.    FailOSErr(myError);
  114.    
  115.    BlockMove(&myPortInfo.name, &myTarget.name, sizeof(PPCPortRec) );
  116.    
  117.    
  118.    myError = AECreateDesc( typeTargetID, (Ptr)(&myTarget), sizeof(TargetID),  
  119.       &theAddressDesc);  
  120.       // loads into address desciptor
  121.    FailOSErr(myError);
  122.    *****/
  123.    
  124.    if (FindHypercardProcessNumber(&process) == FALSE)
  125.       return FALSE;
  126.    
  127.    myError = AECreateDesc(typeProcessSerialNumber, (Ptr)&process, 
  128.       sizeof(process), &theAddressDesc);
  129.    FailOSErr(myError);
  130.  
  131.    myError = AECreateAppleEvent(kHypercardMessageEventClass, kHypercardMessageEventID, 
  132.       &theAddressDesc, kAutoGenerateReturnID, kAnyTransactionID, &theAE); 
  133.    FailOSErr(myError);
  134.    
  135.    myError = AEDisposeDesc( &theAddressDesc);   /* we don't need it anymore */
  136.    FailOSErr(myError);
  137.       
  138.    myError = AEPutParamPtr(&theAE, keyDirectObject, typeChar, 
  139.       messageToSend, strlen(messageToSend) );
  140.    FailOSErr(myError);
  141.    
  142.    sendError = AESend(&theAE, &theReply, kAEQueueReply, kAENormalPriority, 
  143.       kNoTimeOut, myIdleProc, 0L);
  144.    
  145.    if (sendError) {
  146.       myError = AEDisposeDesc(&theAE);
  147.       FailOSErr(sendError);
  148.       return FALSE;
  149.    }
  150.       
  151.    myError = AEDisposeDesc(&theAE);
  152.    FailOSErr(myError);
  153.    
  154.    myError = AEGetParamPtr( &theReply, keyErrorNumber, typeLongInteger, &actualType,
  155.       (Ptr)(&eventError), sizeof(eventError), &actualSize);
  156.    if (myError != errAEDescNotFound) {
  157.       if (myError) {
  158.          myError = AEDisposeDesc( &theReply);
  159.          // SysBeep(1);
  160.          return FALSE;
  161.       } else {
  162.          if (eventError != noErr) {
  163.             myError=AEDisposeDesc( &theReply);
  164.             // SysBeep(1);
  165.             return FALSE;
  166.          }
  167.       }
  168.    }
  169.    
  170.    myError = AEGetParamDesc(&theReply, keyDirectObject, typeChar, 
  171.       &theResultDescriptor);
  172.    if (myError) { /* no direct object exists ! */
  173.       myError = AEDisposeDesc( &theReply);
  174.       // SysBeep(1);
  175.       // FailOSErr(myError);
  176.    }
  177.    
  178.    myError = AESizeOfParam( &theReply, keyDirectObject, &theDescriptorType,
  179.       &theResultSize);
  180.    // FailOSErr(myError);
  181.    
  182.    myError = ZeroScrap();
  183.    // FailOSErr(myError);
  184.    
  185.    myError = PutScrap( theResultSize, 'TEXT', *theResultDescriptor.dataHandle);
  186.    //FailOSErr(myError);
  187.    
  188.    myError = AEDisposeDesc(&theResultDescriptor);
  189.    // FailOSErr(myError);
  190.    
  191.    myError = AEDisposeDesc(&theReply);
  192.    
  193.    return TRUE;
  194. }
  195.  
  196.   
  197. pascal Boolean myIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle mouseRgn)
  198. {
  199.    EventRecord testEvent;
  200.    Boolean gotKbd;
  201.    char key;
  202.    
  203.    gotKbd = GetOSEvent(keyDownMask, &testEvent);
  204.    
  205.    if (gotKbd) {
  206.       key = testEvent.message & charCodeMask;
  207.       if ( (key == '.') && (testEvent.modifiers & cmdKey) )
  208.          return TRUE;
  209.    }
  210.    switch(theEvent->what) {
  211.       case nullEvent:
  212.       break;
  213.    }
  214.    return FALSE;
  215. }
  216.   
  217.  
  218. /*** FIND HYPERCARD PROCESS NUMBER ****/
  219. Boolean FindHypercardProcessNumber(ProcessSerialNumber *process)
  220. {
  221.    OSType sig = kHypercardSignature;
  222.    Boolean found = FALSE;
  223.    ProcessInfoRec pir;
  224.    FSSpec fssp;
  225.    
  226.    found =  FindAProcess(sig, process, &pir, &fssp);
  227.    
  228.    if (found == FALSE) {
  229.       ParamText("\pcouldn't find hypercard running", NULL, NULL, NULL);
  230.       PositionDialog('ALRT', ALRTgeneral);
  231.       InitCursor();
  232.       Alert(ALRTgeneral, NULL);
  233.    }
  234.    return found;
  235. }  
  236.    
  237. /* from InsideMac VI, p. 29-11, "process manager" */
  238.  
  239. Boolean FindAProcess(OSType signature, ProcessSerialNumber *process, 
  240.    ProcessInfoRec *infoRec, FSSpecPtr aFSSpecPtr)
  241. {
  242.    process->highLongOfPSN = 0;
  243.    process->lowLongOfPSN = kNoProcess;  // start from the beginning
  244.    
  245.    infoRec->processInfoLength = sizeof(ProcessInfoRec);
  246.    infoRec->processName = (StringPtr)(NewPtr(32));
  247.    infoRec->processAppSpec = aFSSpecPtr;
  248.    
  249.    while (GetNextProcess(process) == noErr) {
  250.       if (GetProcessInformation(process, infoRec) == noErr) {
  251.          if (infoRec->processType == ((long)'APPL') &&
  252.              infoRec->processSignature == signature) {
  253.             return TRUE;
  254.          }
  255.       }
  256.    }
  257.    SysBeep(1);
  258.    return FALSE;
  259. }
  260.  
  261. -- 
  262. ----------------------------------
  263. #include <std/disclaimer.h>     Josh Rabinowitz, Mac TCL programmer
  264. joshr@kronos.arc.nasa.gov
  265. "Send a salami to your boy in the army" - Katz's delicatessen, NYC
  266.