home *** CD-ROM | disk | FTP | other *** search
- /* $Header: tyrathect:Development:Perl::RCS:missing.c,v 1.2 1994/05/04 02:12:43 neeri Exp $
- *
- * Copyright (c) 1995 Matthias Neeracher
- *
- * You may distribute under the terms of the Perl Artistic License,
- * as specified in the README file.
- *
- * $Log: missing.c,v $
- */
-
- #define MAC_CONTEXT
-
- #include "EXTERN.h"
- #include "perl.h"
- #include "XSUB.h"
- #include <Types.h>
- #include <Components.h>
- #include <AppleEvents.h>
- #include <AppleScript.h>
- #include <OSA.h>
- #include <Gestalt.h>
-
- ComponentInstance gScriptingComponent;
-
- void ShutDownAppleScript(void)
- {
- CloseComponent(gScriptingComponent);
-
- gScriptingComponent = nil;
- }
-
- OSErr InitAppleScript(void)
- {
- OSErr myErr;
- ComponentDescription descr;
- ComponentDescription capabilities;
- Component myComponent;
- EventRecord myEvent;
- short retryCount;
- long res;
-
- retryCount = 0;
-
- if (Gestalt(gestaltAppleEventsAttr, &res))
- return -1;
- else if (!(res & (1<<gestaltAppleEventsPresent)))
- return -1;
- else if (Gestalt(gestaltComponentMgr, &res))
- return -1;
-
- descr.componentType = kOSAComponentType;
- descr.componentSubType = kAppleScriptSubtype;
- descr.componentManufacturer = (OSType) 0;
- descr.componentFlags = kOSASupportsCompiling +
- kOSASupportsGetSource +
- kOSASupportsAESending;
- descr.componentFlagsMask = descr.componentFlags;
-
- myComponent = FindNextComponent(nil, &descr);
-
- if (myComponent==nil)
- return -1;
- else {
- myErr = GetComponentInfo(myComponent, &capabilities, nil, nil, nil);
- gScriptingComponent = OpenComponent(myComponent);
- if (!gScriptingComponent)
- return(-1);
- else
- atexit(ShutDownAppleScript);
- }
-
- return myErr;
- }
-
- XS(XS_MacPerl_MP_Reply)
- {
- dXSARGS;
- if (items != 1) {
- croak("Usage: MacPerl::Reply(reply)");
- }
- {
- char * reply = (char *)SvPV(ST(0),na);
- {
- if (gPerlReply)
- DisposeHandle(gPerlReply);
- /**/
- PtrToHand(reply, &gPerlReply, strlen(reply));
- }
- }
- XSRETURN(1);
- }
-
- XS(XS_MacPerl_MP_DoAppleScript)
- {
- dXSARGS;
- if (items != 1) {
- croak("Usage: MacPerl::DoAppleScript(script)");
- }
- {
- SV * script = ST(0);
- {
- AEDesc source;
- AEDesc result;
- char * scriptText;
- STRLEN len;
- /**/
- if (!gScriptingComponent && InitAppleScript())
- croak("MacPerl::DoAppleScript couldn't initialize AppleScript");
- /**/
- scriptText = (char*) SvPV(ST(0), len);
- AECreateDesc(typeChar, scriptText, len, &source);
- /**/
- if (!OSADoScript(
- gScriptingComponent,
- &source,
- kOSANullScript,
- typeChar,
- kOSAModeCanInteract,
- &result))
- {
- AEDisposeDesc(&source);
- /**/
- if (!AECoerceDesc(&result, typeChar, &source)) {
- HLock(source.dataHandle);
- ST(0) = sv_2mortal(newSVpv(*source.dataHandle,GetHandleSize(source.dataHandle)));
- AEDisposeDesc(&source);
- } else
- ST(0) = &sv_undef;
- /**/
- AEDisposeDesc(&result);
- } else {
- AEDisposeDesc(&source);
- /**/
- ST(0) = &sv_undef;
- }
- }
- }
- XSRETURN(1);
- }
-
- XS(boot_OSA)
- {
- dXSARGS;
- char* file = __FILE__;
-
- newXS("MacPerl::Reply", XS_MacPerl_MP_Reply, file);
- newXS("MacPerl::DoAppleScript", XS_MacPerl_MP_DoAppleScript, file);
- ST(0) = &sv_yes;
- XSRETURN(1);
- }
-