home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacPerl 5.0.3 / MacPerl Source ƒ / Perl5 / ext / MacPerl / OSA.xs < prev    next >
Encoding:
Text File  |  1995-08-24  |  2.7 KB  |  128 lines  |  [TEXT/MPS ]

  1. /* $Header: tyrathect:Development:Perl::RCS:missing.c,v 1.2 1994/05/04 02:12:43 neeri Exp $
  2.  *
  3.  *    Copyright (c) 1995 Matthias Neeracher
  4.  *
  5.  *    You may distribute under the terms of the Perl Artistic License,
  6.  *    as specified in the README file.
  7.  *
  8.  * $Log: missing.c,v $
  9.  */
  10.  
  11. #define MAC_CONTEXT
  12.  
  13. #include "EXTERN.h"
  14. #include "perl.h"
  15. #include "XSUB.h"
  16. #include <Types.h>
  17. #include <Components.h>
  18. #include <AppleEvents.h>
  19. #include <AppleScript.h>
  20. #include <OSA.h>
  21. #include <Gestalt.h>
  22.  
  23. ComponentInstance gScriptingComponent;
  24.  
  25. void ShutDownAppleScript(void)
  26. {
  27.     CloseComponent(gScriptingComponent);
  28.     
  29.     gScriptingComponent = nil;
  30. }
  31.  
  32. OSErr InitAppleScript(void)
  33. {
  34.     OSErr                myErr;
  35.     ComponentDescription descr;
  36.     ComponentDescription capabilities;
  37.     Component            myComponent;
  38.     EventRecord          myEvent;
  39.     short                retryCount;
  40.     long                        res;
  41.             
  42.     retryCount = 0;
  43.     
  44.     if (Gestalt(gestaltAppleEventsAttr, &res))
  45.         return -1;
  46.     else if (!(res & (1<<gestaltAppleEventsPresent)))
  47.         return -1;
  48.     else if (Gestalt(gestaltComponentMgr, &res))
  49.         return -1;
  50.         
  51.     descr.componentType         = kOSAComponentType;
  52.     descr.componentSubType      = kAppleScriptSubtype;
  53.     descr.componentManufacturer = (OSType) 0;
  54.     descr.componentFlags        = kOSASupportsCompiling + 
  55.                                             kOSASupportsGetSource + 
  56.                                             kOSASupportsAESending;
  57.     descr.componentFlagsMask    = descr.componentFlags;
  58.     
  59.     myComponent = FindNextComponent(nil, &descr);
  60.     
  61.     if (myComponent==nil)
  62.           return -1;
  63.     else {
  64.         myErr = GetComponentInfo(myComponent, &capabilities, nil, nil, nil);
  65.         gScriptingComponent = OpenComponent(myComponent);
  66.         if (!gScriptingComponent)
  67.             return(-1);
  68.         else
  69.             atexit(ShutDownAppleScript);
  70.     }
  71.         
  72.     return myErr;
  73. }
  74.  
  75. MODULE = OSA    PACKAGE = MacPerl    PREFIX = MP_
  76.  
  77. void
  78. MP_Reply(reply)
  79.     char *    reply
  80.     CODE:
  81.     {
  82.         if (gPerlReply)
  83.             DisposeHandle(gPerlReply);
  84.     /**/        
  85.         PtrToHand(reply, &gPerlReply, strlen(reply));
  86.     }
  87.  
  88. void
  89. MP_DoAppleScript(script)
  90.     SV *    script
  91.     CODE:
  92.     {
  93.         AEDesc    source;
  94.         AEDesc    result;
  95.         char *    scriptText;
  96.         STRLEN    len;
  97.     /**/        
  98.         if (!gScriptingComponent && InitAppleScript())
  99.             croak("MacPerl::DoAppleScript couldn't initialize AppleScript");
  100.     /**/        
  101.         scriptText = (char*) SvPV(ST(0), len);
  102.         AECreateDesc(typeChar, scriptText, len, &source);
  103.     /**/        
  104.         if (!OSADoScript(
  105.                 gScriptingComponent, 
  106.                 &source, 
  107.                 kOSANullScript, 
  108.                 typeChar, 
  109.                 kOSAModeCanInteract,
  110.                 &result))
  111.         {
  112.             AEDisposeDesc(&source);
  113.     /**/        
  114.             if (!AECoerceDesc(&result, typeChar, &source)) {
  115.                 HLock(source.dataHandle);
  116.                 ST(0) = sv_2mortal(newSVpv(*source.dataHandle,GetHandleSize(source.dataHandle)));
  117.                 AEDisposeDesc(&source);
  118.             } else
  119.                 ST(0) = &sv_undef;
  120.     /**/        
  121.             AEDisposeDesc(&result);
  122.         } else {
  123.             AEDisposeDesc(&source);
  124.     /**/        
  125.             ST(0) = &sv_undef;
  126.         }
  127.     }
  128.