home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 19 / develop 19 code / SimpliFace_V2 / Sources / ScriptUtils.cp < prev    next >
Encoding:
Text File  |  1994-05-01  |  12.4 KB  |  564 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ScriptUtils.cp
  3.  
  4.     Contains:    Script handling & OSA interface
  5.  
  6.  
  7.     Developed by:
  8.  
  9.     Paul G Smith (commstalk hq & Full Moon Software, Inc)
  10.  
  11.     you can leave messages at (UK): 0727 844232; (US): 408 253 7199
  12.     BUT I prefer to be contacted by e-mail
  13.     AppleLink:     COMMSTALK.HQ
  14.     Internet:     COMMSTALK.HQ@applelink.apple.com
  15.  
  16.     "SimpliFace2" Sample code to accompany develop article
  17.     on techniques for controlling script inheritance.
  18.     
  19.     
  20.  
  21.  
  22.     
  23.  
  24. */
  25.  
  26.  
  27. #include "ScriptUtils.h"
  28. #include "SimpliFace2Common.h"
  29.  
  30. #include    <StdArg.h>
  31. #include <Sysequ.h>
  32.  
  33. #ifndef __LIMITS__
  34. #include <Limits.h>
  35. #endif
  36.  
  37. #ifndef __ERRORS__
  38. #include <Errors.h>
  39. #endif
  40.  
  41. #ifndef __RESOURCES__
  42. #include <Resources.h>
  43. #endif
  44.  
  45. #ifndef __PACKAGES__
  46. #include <Packages.h>
  47. #endif
  48.  
  49. #ifndef __PROCESSES__
  50. #include <Processes.h>
  51. #endif
  52.  
  53. #ifndef __PLSTRINGFUNCS__
  54. #include <PLStringFuncs.h>
  55. #endif
  56.  
  57. #ifndef __AEOBJECTS__
  58. #include <AEObjects.h>
  59. #endif
  60.  
  61. #ifndef __ASDEBUGGING__
  62. #include <ASDebugging.h>
  63. #endif
  64.  
  65. #ifndef __ASREGISTRY__
  66. #include <ASRegistry.h>
  67. #endif
  68. #ifndef __WINDOWS__
  69. #include <Windows.h>
  70. #endif
  71.  
  72.  
  73. #pragma trace off
  74.  
  75.  
  76.  
  77. pascal OSErr GetCStringFromDescriptor(const AEDesc *sourceDesc, CStr255 *resultStr)
  78.   {
  79.           OSErr       err;
  80.         Size        stringSize = 0;
  81.         AEDesc      resultDesc;
  82.         char        buff[260];
  83.  
  84.         resultDesc.dataHandle = nil;
  85.         
  86.         *resultStr = "";
  87.         
  88.         err = AECoerceDesc(sourceDesc, typeChar, &resultDesc);
  89.         
  90.         if (err == noErr) 
  91.             GetRawDataFromDescriptor(&resultDesc, (Ptr)buff,
  92.                                      255, &stringSize);
  93.         if (stringSize<256)
  94.         {
  95.             buff[stringSize] = 0;
  96.             *resultStr = buff;
  97.         }
  98.         else
  99.             err = errAECoercionFail;
  100.             
  101.         if (resultDesc.dataHandle) 
  102.             AEDisposeDesc(&resultDesc);
  103.             
  104.         return err;
  105.     }
  106.  
  107.  
  108.  
  109.  
  110. OSErr    MakeNullDesc(AEDesc *dataDesc)
  111. {
  112.     return AECreateDesc(typeNull, NULL, 0, dataDesc);
  113. }
  114.  
  115.  
  116. OSErr    MakeNameDesc(CStr255& theString, AEDesc *dataDesc)
  117. {
  118.     return AECreateDesc(typeChar, (char*)theString, 
  119.                         theString.Length(), dataDesc);
  120. }
  121.  
  122.  
  123. OSErr    MakeLongIntDesc(long theIndex, AEDesc *dataDesc)
  124. {
  125.     return AECreateDesc(typeLongInteger, &theIndex, sizeof(long), dataDesc);
  126. }
  127.  
  128.  
  129.  
  130. Boolean    ScriptIsScriptContext(ComponentInstance scriptingComponent,
  131.                                 OSAID contextID)
  132. {
  133.     OSAError        err = 0;
  134.     long            result = false;
  135.     
  136.     err = OSAGetScriptInfo(scriptingComponent, contextID,
  137.                             kOSAScriptIsTypeScriptContext, &result);
  138.     if (err == noErr)
  139.         return (Boolean)result;
  140.     else
  141.         return false;
  142.     
  143. }
  144.  
  145.  
  146. //----------------------------------------------------------------------------------//
  147. //    Initializes all descriptor records passed in to this routine.  The variable        //
  148. //    argument list is null terminated.                                                //
  149. //----------------------------------------------------------------------------------//
  150. #pragma segment Utils
  151. void InitAEDescs(AEDesc*  desc1, ... )            // Variable, null terminated argument list.
  152. {
  153.     va_list        argptr;                            // pointer to each argument in list.
  154.     AEDesc*        nextDesc;                        // next descriptor argument in list.
  155.  
  156.     va_start(argptr, desc1);
  157.     desc1->descriptorType = typeNull;
  158.     desc1->dataHandle = nil;
  159.     
  160.     while((nextDesc = va_arg(argptr, AEDesc *)))
  161.     {
  162.         nextDesc->descriptorType = typeNull;
  163.         nextDesc->dataHandle = nil;
  164.     }
  165.     va_end(argptr);
  166. }
  167.     
  168. //----------------------------------------------------------------------------------//
  169. //    Dispose all descriptor records passed into this routine. (Variable arg. list).    //
  170. //----------------------------------------------------------------------------------//
  171. #pragma segment Utils
  172. void DisposeAEDescs(AEDesc*  desc1, ... )        // Null terminated argument list.
  173. {
  174.     va_list        argptr;            // pointer to each argument in list.
  175.     AEDesc*        nextDesc;        // descriptor argument in list.
  176.  
  177.     va_start(argptr, desc1);
  178.     if (desc1->dataHandle)
  179.         AEDisposeDesc(desc1);
  180.  
  181.     while((nextDesc = va_arg(argptr, AEDesc *)))
  182.     {
  183.         if (nextDesc->dataHandle)
  184.             AEDisposeDesc(nextDesc);
  185.     }
  186.     va_end(argptr);
  187. }
  188.  
  189. /**-----------------------------------------------------------------------
  190.         Name:             LesserOf
  191.         Purpose:        Returns the Lesser of two longints.
  192.     -----------------------------------------------------------------------**/
  193. #pragma segment Utils
  194.         
  195. pascal long LesserOf(long A, long B)
  196.  {
  197.    if (A<B)
  198.        return(A);
  199.      else
  200.        return(B);
  201.  }   /*LesserOf*/
  202.             
  203. /**-----------------------------------------------------------------------
  204.         Name:             GreaterOf
  205.         Purpose:        Returns the Greater of two longints.
  206.     -----------------------------------------------------------------------**/
  207.     
  208. #pragma segment Utils
  209.         
  210. pascal long GreaterOf(long A, long B)
  211.  {
  212.    if (A>B)
  213.        return(A);
  214.      else
  215.        return(B);
  216.  }  /*GreaterOf*/
  217.             
  218.  
  219. /**-----------------------------------------------------------------------
  220.     Utility Routines for getting data from AEDesc's
  221.   -----------------------------------------------------------------------**/
  222.     
  223. pascal void GetRawDataFromDescriptor(const AEDesc *theDesc,
  224.                                                                          Ptr     destPtr,
  225.                                                                          Size    destMaxSize,
  226.                                                                          Size    *actSize)
  227.   {
  228.       Size copySize;
  229.  
  230.  
  231.         if (theDesc->dataHandle) 
  232.             {
  233.                 HLock((Handle)theDesc->dataHandle);
  234.                 *actSize = GetHandleSize((Handle)theDesc->dataHandle);
  235.                 
  236.                 copySize = LesserOf(*actSize, destMaxSize);
  237.                 
  238.                 BlockMove(*theDesc->dataHandle, destPtr, copySize);
  239.                 
  240.                 HUnlock((Handle)theDesc->dataHandle);
  241.             }
  242.         else
  243.             *actSize = 0;
  244.     } /*GetRawDataFromDescriptor*/
  245.  
  246. pascal OSErr GetPStringFromDescriptor(const AEDesc *sourceDesc, char *resultStr)
  247.  
  248.   {
  249.       OSErr        myErr;
  250.         OSErr                 ignoreErr;
  251.         Size         stringSize;
  252.         AEDesc       resultDesc;
  253.  
  254.         resultDesc.dataHandle = nil;
  255.         
  256.         resultStr[0] = 0;
  257.         
  258.         myErr = AECoerceDesc(sourceDesc,typeChar,&resultDesc);
  259.         
  260.         if (myErr==noErr) 
  261.             GetRawDataFromDescriptor(&resultDesc,
  262.                                                              (Ptr)&resultStr[1],
  263.                                                              250,
  264.                                                              &stringSize);
  265.         if (stringSize<250) 
  266.             resultStr[0] = (char)stringSize;
  267.         else
  268.             resultStr[0] = (char)250;
  269.             
  270.         if (resultDesc.dataHandle) 
  271.             ignoreErr = AEDisposeDesc(&resultDesc);
  272.             
  273.         return(myErr);
  274.     }
  275.  
  276. pascal OSErr GetIntegerFromDescriptor(const AEDesc *sourceDesc, short *result)
  277.   {
  278.       OSErr   myErr;
  279.         OSErr   ignoreErr;
  280.         Size    intSize;
  281.         AEDesc  resultDesc;
  282.         
  283.         *result = 0;
  284.         myErr  = AECoerceDesc(sourceDesc,typeShortInteger,&resultDesc);
  285.         
  286.         if (myErr==noErr) 
  287.             {
  288.                 GetRawDataFromDescriptor(&resultDesc,
  289.                                                                  (Ptr)result,
  290.                                                                  2,
  291.                                                                  &intSize);
  292.                 if (intSize>2) 
  293.                     myErr = errAECoercionFail;
  294.             }
  295.         
  296.         if (resultDesc.dataHandle) 
  297.             ignoreErr = AEDisposeDesc(&resultDesc);
  298.             
  299.         return(myErr);
  300.     }
  301.     
  302.     
  303. pascal OSErr GetDescTypeFromDescriptor(const AEDesc *sourceDesc, DescType *result)
  304.   {
  305.       OSErr   myErr;
  306.         OSErr   ignoreErr;
  307.         Size    descSize;
  308.         AEDesc  resultDesc;
  309.         
  310.         *result = typeNull;
  311.         myErr  = AECoerceDesc(sourceDesc,typeShortInteger,&resultDesc);
  312.         
  313.         if (myErr==noErr) 
  314.             {
  315.                 GetRawDataFromDescriptor(&resultDesc,
  316.                                                                  (Ptr)result,
  317.                                                                  sizeof(DescType),
  318.                                                                  &descSize);
  319.                 if (descSize != sizeof(DescType)) 
  320.                     myErr = errAECoercionFail;
  321.             }
  322.         
  323.         if (resultDesc.dataHandle) 
  324.             ignoreErr = AEDisposeDesc(&resultDesc);
  325.             
  326.         return(myErr);
  327.     }
  328.     
  329. pascal OSErr GetBooleanFromDescriptor(const AEDesc *sourceDesc,
  330.                                                                         Boolean *result)
  331.   {
  332.       OSErr  myErr;
  333.         OSErr  ignoreErr;
  334.         Size   boolSize;
  335.         AEDesc resultDesc;
  336.         
  337.         *result = false;
  338.         myErr = AECoerceDesc(sourceDesc,typeBoolean,&resultDesc);
  339.         
  340.         if (myErr==noErr) 
  341.             {
  342.                 GetRawDataFromDescriptor(&resultDesc,
  343.                                                                  (Ptr)result,
  344.                                                                  sizeof(Boolean),
  345.                                                                  &boolSize);
  346.                 if (boolSize>sizeof(Boolean)) 
  347.                     myErr = errAECoercionFail;
  348.             }
  349.         
  350.         if (resultDesc.dataHandle) 
  351.             ignoreErr = AEDisposeDesc(&resultDesc);
  352.             
  353.         return(myErr);
  354.     }
  355.  
  356. pascal OSErr GetLongIntFromDescriptor(const AEDesc *sourceDesc, 
  357.                                       long   *result)
  358.   {
  359.       OSErr   myErr;
  360.         OSErr   ignoreErr;
  361.         Size    intSize;
  362.         AEDesc  resultDesc;
  363.         
  364.         *result = 0;
  365.         myErr = AECoerceDesc(sourceDesc,typeLongInteger,&resultDesc);
  366.         
  367.         if (myErr==noErr) 
  368.             {
  369.                 GetRawDataFromDescriptor(&resultDesc,
  370.                                                                  (Ptr)result,
  371.                                                                  4,
  372.                                                                  &intSize);
  373.                 if (intSize>4) 
  374.                     myErr = errAECoercionFail;
  375.             }
  376.         
  377.         if (resultDesc.dataHandle) 
  378.             ignoreErr = AEDisposeDesc(&resultDesc);
  379.             
  380.         return(myErr);
  381.     } /*GetLongIntFromDescriptor*/
  382.  
  383. pascal OSErr GetRectFromDescriptor(const AEDesc *sourceDesc, Rect *result)
  384.     {
  385.         OSErr   myErr;
  386.         OSErr   ignoreErr;
  387.         Size    rectSize;
  388.         AEDesc  resultDesc;
  389.             
  390.         SetRect(result,0,0,0,0);
  391.         myErr = AECoerceDesc(sourceDesc,typeQDRectangle,&resultDesc);
  392.         
  393.         if (myErr==noErr) 
  394.             {
  395.                 GetRawDataFromDescriptor(&resultDesc,
  396.                                                                  (Ptr)result,
  397.                                                                  sizeof(Rect),
  398.                                                                  &rectSize);
  399.                 if (rectSize<sizeof(Rect)) 
  400.                     myErr = errAECoercionFail;
  401.             }
  402.         
  403.         if (resultDesc.dataHandle) 
  404.             ignoreErr = AEDisposeDesc(&resultDesc);
  405.             
  406.         return(myErr);
  407.     } /*GetRectFromDescriptor*/
  408.  
  409. pascal OSErr GetPointFromDescriptor(const AEDesc *sourceDesc,
  410.                                                                   Point  *result)
  411.   {
  412.       OSErr   myErr;
  413.         OSErr   ignoreErr;
  414.         Size    ptSize;
  415.         AEDesc  resultDesc;
  416.         
  417.         SetPt(result,0,0);
  418.         
  419.         myErr = AECoerceDesc(sourceDesc,typeQDPoint,&resultDesc);
  420.         
  421.         if (myErr==noErr) 
  422.             {
  423.                 GetRawDataFromDescriptor(&resultDesc,
  424.                                                                  (Ptr)result,
  425.                                                                  sizeof(Point),
  426.                                                                  &ptSize);
  427.                                                                  
  428.                 if (ptSize<sizeof(Point)) 
  429.                     myErr = errAECoercionFail;
  430.                     
  431.             }
  432.         
  433.         if (resultDesc.dataHandle) 
  434.             ignoreErr = AEDisposeDesc(&resultDesc);
  435.             
  436.         return(myErr);
  437.     } /*GetPointFromDescriptor*/
  438.  
  439.  
  440. /*******************************************************************************/
  441. /*
  442.     Object Accessors - Utility Routines
  443. */
  444.  
  445. #pragma segment ObjectAccessors
  446.  
  447. pascal WindowPtr WindowNameToWindowPtr(StringPtr nameStr)
  448. /* 
  449.     Returns the WindowPtr of the window with title nameStr
  450.     or nil if there is no matching window.
  451. */
  452.     { 
  453.         WindowPtr theWindow;
  454.         Str255    windTitle;
  455.             
  456.         theWindow = (WindowPtr)*((Handle)WindowList);
  457.         /* 
  458.             iterate through windows - we use WindowList 'cos we could
  459.             have made the window invisible and  we lose it - so we
  460.             can't set it back to visible!!
  461.         */
  462.         while (theWindow)
  463.             {
  464.                 GetWTitle(theWindow, windTitle);
  465.                 if (EqualString(windTitle,
  466.                                 nameStr,
  467.                                                 false,
  468.                                                 true))     /* ignore case, don't ignore diacriticals */
  469.                     return(theWindow);
  470.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  471.             }
  472.         return(theWindow);
  473.     }    /* WindowNameToWindowPtr */
  474.  
  475. pascal WindowPtr GetWindowPtrOfNthWindow(short index)
  476. /* returns a ptr to the window with the given index
  477.   (front window is 1, behind that is 2, etc.).  if
  478.   there's no window with that index (inc. no windows
  479.   at all), returns nil.
  480. */
  481.   {
  482.       WindowPtr theWindow;
  483.         
  484.         theWindow = (WindowPtr)*((Handle)WindowList);
  485.     
  486.         /* iterate through windows */
  487.         
  488.         while (theWindow)
  489.             {
  490.                 index --;
  491.                 if (index <= 0) 
  492.                     return(theWindow);
  493.                     
  494.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  495.             }
  496.         return(nil);
  497.     }    /* GetWindowPtrOfNthWindow */
  498.     
  499. pascal short CountWindows(void)
  500.     {
  501.         WindowPtr theWindow;
  502.         short     index;
  503.                 
  504.         index = 0;
  505.         theWindow = (WindowPtr)*((Handle)WindowList);
  506.     
  507.         /* iterate through windows */
  508.         
  509.         while (theWindow)
  510.             {
  511.                 index++;                    
  512.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  513.             }
  514.         
  515.         return(index);
  516.         
  517.     } /*CountWindows*/
  518.  
  519.  
  520.     
  521. /* Next bit of code nabbed from SignatureToApp */
  522.  
  523. pascal OSErr FindApplicationOnVolume(OSType whatSig, short theVol, FSSpec *foundSpec)
  524.     {
  525.       OSErr                  myErr;
  526.         DTPBRec                myPB;
  527.         HParamBlockRec         myHParams;
  528.         GetVolParmsInfoBuffer  theVolInfo;
  529.         
  530.         myHParams.ioParam.ioCompletion = nil;
  531.         myHParams.ioParam.ioNamePtr    = nil;
  532.         myHParams.ioParam.ioVRefNum    = theVol;
  533.         myHParams.ioParam.ioBuffer     = (Ptr)&theVolInfo;
  534.         myHParams.ioParam.ioReqCount   = sizeof(theVolInfo);
  535.         
  536.         myErr = PBHGetVolParms(&myHParams, false); /* synchchronous */
  537.  
  538.         if (myErr==noErr && (theVolInfo.vMAttrib & (1 << bHasDesktopMgr)) != 0)
  539.             { /* Have a deskTop database */
  540.                 myPB.ioCompletion = nil;
  541.                 myPB.ioVRefNum    = theVol;
  542.                 myPB.ioNamePtr    = nil;
  543.                 
  544.                 myErr = PBDTGetPath(&myPB); /* Gets myPB.ioDTRefNum */
  545.                 
  546.                 if (myErr==noErr)
  547.                   {
  548.                         myPB.ioCompletion = nil;
  549.                         myPB.ioIndex      = 0;
  550.                         myPB.ioFileCreator= whatSig;
  551.                         myPB.ioNamePtr    = (StringPtr)&foundSpec->name[0];
  552.                         
  553.                         myErr = PBDTGetAPPL(&myPB, false);
  554.                         
  555.                         if (myErr == noErr)
  556.                             {
  557.                                 foundSpec->vRefNum = theVol;
  558.                                 foundSpec->parID   = myPB.ioAPPLParID;
  559.                             }
  560.                   }                
  561.             }        
  562.         return(myErr);
  563.     }
  564.