home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / Script Builder 1.0 / Helper Source / GOSAScript.cp < prev    next >
Encoding:
Text File  |  1995-12-12  |  4.4 KB  |  223 lines  |  [TEXT/CWIE]

  1. /***
  2.  * GOSAScript.cp
  3.  *
  4.  *  Methods to implement a script access.
  5.  *
  6.  *  Gordon Watts (gwatts@fnal.fnal.gov) © 1995 As Is!
  7.  ***/
  8.  
  9. #include "GOSAScriptComponent.h"
  10. #include "GOSAScript.h"
  11. #include "GOSAID.h"
  12. #include "StAERecord.h"
  13. #include "debug.h"
  14.  
  15. /**
  16.  * GOSAScript
  17.  *
  18.  *  Buidl the object up.
  19.  *
  20.  **/
  21. GOSAScript :: GOSAScript (const GOSAScriptComponent *theComponent)
  22.                 : GOSAID (theComponent)
  23. {
  24.     mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
  25. }
  26. GOSAScript :: GOSAScript (void)
  27. {
  28.     mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
  29. }
  30.  
  31. GOSAScript :: GOSAScript (const GOSAScriptComponent *theComponent,
  32.                             const OSAID theId)
  33.                 : GOSAID (theComponent, theId)
  34. {
  35.     mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
  36. }
  37. GOSAScript :: GOSAScript (const OSAID theId)
  38.                 : GOSAID (theId)
  39. {
  40.     mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
  41. }
  42. GOSAScript :: GOSAScript (const GOSAScriptComponent *theComponent,
  43.                             const FSSpec &theFile)
  44.                 : GOSAID (theComponent)
  45. {
  46.     mScriptContext = new GOSAID (theComponent, kOSANullScript);
  47.     LoadScript (theFile);
  48. }
  49. GOSAScript :: GOSAScript (const FSSpec &theFile)
  50. {
  51.     mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
  52.     LoadScript (theFile);
  53. }
  54.  
  55. /**
  56.  * ~GOSAScript
  57.  *
  58.  *  Delete everything!
  59.  *
  60.  **/
  61. GOSAScript :: ~GOSAScript (void)
  62. {
  63.     if (mScriptContext)
  64.         delete mScriptContext;
  65. }
  66.  
  67. /**
  68.  * LoadScript
  69.  *
  70.  *  Load a compiled script from the given fsspec.  Bomb if we can't seem to get
  71.  *  one in!
  72.  *
  73.  **/
  74. void GOSAScript :: LoadScript (const FSSpec &theSpec)
  75. {
  76.     /**
  77.      ** Get the resource handle for the script.
  78.      **/
  79.  
  80.     short    fileRef;
  81.     
  82.     fileRef = FSpOpenResFile (&theSpec, fsRdPerm);
  83.     ThrowIfOSErr_(ResError());
  84.     
  85.     Handle    scriptHandle;
  86.  
  87.     scriptHandle = Get1Resource (kOSAScriptResourceType, 128);
  88.     ThrowIfNil_(scriptHandle);
  89.  
  90.     /**
  91.      ** Build a AEDesc out of it, and then get it loaded.
  92.      **/
  93.  
  94.     Try_ {
  95.         AEDesc        scriptDesc;
  96.     
  97.         scriptDesc.descriptorType = typeOSAGenericStorage;
  98.         scriptDesc.dataHandle = scriptHandle;
  99.     
  100.     /**
  101.      ** Get a second routine to do the work!
  102.      **/
  103.     
  104.         LoadScript (scriptDesc);
  105.     
  106.     } Catch_(err) {
  107.         if (scriptHandle)
  108.             ReleaseResource (scriptHandle);
  109.         CloseResFile (fileRef);
  110.         Throw_(err);
  111.     }
  112.  
  113.     ReleaseResource (scriptHandle);
  114.     CloseResFile (fileRef);
  115. }
  116.  
  117. /**
  118.  * LoadScript
  119.  *
  120.  *   The contents of the script are in a AEDesc -- load the damm thing.
  121.  *   Save the id so we can execute the script later.  Mark the id as valid
  122.  *   if the load went ok.
  123.  *
  124.  **/
  125. void GOSAScript :: LoadScript (const AEDesc &theScript)
  126. {
  127.     OSAError    err;
  128.     
  129.     err = OSALoad (mScriptingComponent->instance(),
  130.             &theScript, kOSAModeNull, &mID);
  131.     mIDValid = err == noErr;
  132. }
  133.  
  134. /**
  135.  * Execute
  136.  *
  137.  *  Run the script -- shove the returned id into an GOSAID.
  138.  *
  139.  **/
  140. void GOSAScript :: Execute (GOSAID &theResult)
  141. {
  142.     AssertStr (mIDValid, "\pAttempt to execute script with invalid id!");
  143.  
  144.     /**
  145.      ** Make sure we have something to execute.
  146.      **/
  147.  
  148.     theResult.SetID (kOSANullScript);
  149.  
  150.     if (mID != kOSANullScript) {
  151.         OSAError    err;
  152.         OSAID        theResultID;
  153.  
  154.         err = OSAExecute (mScriptingComponent->instance(),  mID,
  155.                 //mScriptContext->id(),
  156.                 kOSANullScript, kOSAModeNull, &theResultID);
  157.         ThrowIfOSErr_(err);
  158.  
  159.         if (theResultID != kOSANullScript)
  160.             theResult.SetID (theResultID);
  161.     }
  162. }
  163.  
  164. void GOSAScript :: Execute (AEDesc &theResult)
  165. {
  166.     GOSAID    theResultObj (mScriptingComponent);
  167.     
  168.     Execute (theResultObj);
  169.  
  170.     theResultObj.GetAEDesc (theResult);
  171. }
  172.  
  173. void GOSAScript :: Execute (void)
  174. {
  175.     StAEDescriptor    temp;
  176.     
  177.     Execute (temp.mDesc);
  178. }
  179.  
  180. /**
  181.  * DoEvent
  182.  *
  183.  *  Execute an event in this context.
  184.  *
  185.  **/
  186. void GOSAScript :: DoEvent (const AppleEvent &theEvent, AppleEvent &theReply) const
  187. {
  188.     OSAError    err;
  189.     
  190.     AssertStr (mIDValid, "\pAttempt to execute invalid id script");
  191.     
  192.     err = OSADoEvent (mScriptingComponent->instance (),
  193.             &theEvent, mID, kOSAModeNull, &theReply);
  194.     
  195.     ThrowIfOSErr_ (err);
  196. }
  197.  
  198. /**
  199.  * LoadFromResource
  200.  *
  201.  *  We have a script resource -- load in the damm script!
  202.  *
  203.  **/
  204. void GOSAScript :: LoadFromResource (const StringPtr resourceName, const OSType resType)
  205. {
  206.     StResource scriptResource (resType, resourceName, true, true);
  207.     
  208.     LoadFromHandle (scriptResource);
  209. }
  210.  
  211. /**
  212.  * LoadFromHandle
  213.  *
  214.  *  Given a handle containing the script data (like a resource handle),
  215.  *  remember it!
  216.  **/
  217. void GOSAScript :: LoadFromHandle (const Handle theScriptHandle)
  218. {
  219.     const AEDesc    scriptData = {typeOSAGenericStorage, theScriptHandle};
  220.     
  221.     LoadScript (scriptData);
  222. }
  223.