AppleEventStuff.c

/*
    File:       AppleEventStuff.c
 
    Contains:   Handlers for the 4 "required" events
 
    Written by: Chris White 
 
    Copyright:  Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#ifndef __MIXEDMODE__
    #include <MixedMode.h>
#endif
 
#ifndef __GESTALT__
    #include <Gestalt.h>
#endif
 
#ifndef __APPLEEVENTS__
    #include <AppleEvents.h>
#endif
 
 
 
 
 
#ifndef __FRAGMENTTOOL__
    #include "FragmentTool.h"
#endif
 
#ifndef __PROTOTYPES__
    #include "Prototypes.h"
#endif
 
#ifndef __APPLEEVENTSTUFF__
    #include "AppleEventStuff.h"
#endif
 
 
 
 
 
//
// Prototypes for those routines which are private to this source file
//
 
static pascal OSErr HandleOapp ( AEDescList* aevt, AEDescList* reply, long refCon );
static pascal OSErr HandleQuit ( AEDescList* aevt, AEDescList* reply, long refCon );
static pascal OSErr HandleOdoc ( AEDescList* aevt, AEDescList* reply, long refCon );
static pascal OSErr HandlePdoc ( AEDescList* aevt, AEDescList* reply, long refCon );
 
 
 
 
void InstallAppleEventHandlers ( void )
{
    OSErr   theErr;
    int32   theResult;
 
    theErr = Gestalt ( gestaltAppleEventsAttr, &theResult );
    if ( theErr == noErr )
    {   
        AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc ( HandleOapp ), 0, false );
        AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc ( HandleOdoc ), 0, false );
        AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc ( HandlePdoc ), 0, false );
        AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc ( HandleQuit ), 0, false );
    }
    
    return;
    
}   // InstallAppleEventHandlers
 
 
//
// When we get an "oapp" event, we've been launched without any
// document to open. So, we'll create an untitled document.
//
pascal OSErr HandleOapp ( AEDescList* aevt, AEDescList* reply, long refCon )
{
    #pragma unused(aevt,reply,refCon)
    DoNewDocument ( );
    
    return noErr;
}
 
 
 
//
// The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater.
// This means that we'll need to extract the list, count the list's elements, and
// then process each file in turn.
//                                                                                               */
pascal OSErr HandleOdoc ( AEDescList* aevt, AEDescList* reply, long refCon )
{
    #pragma unused(reply,refCon)
    AEDesc      fileListDesc = {'NULL', NULL};
    long        numFiles;
    DescType    actualType;
    long        actualSize;
    AEKeyword   actualKeyword;
    FSSpec      oneFile;
    long        index;
    OSErr       err;
                        
    
    // Extract the list of aliases into fileListDesc
    err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
    if ( err ) goto CleanupAndBail;
        
    // Count the list elements
    err = AECountItems( &fileListDesc, &numFiles);
    if ( err ) goto CleanupAndBail;
    
    // Now get each file from the list and process it.
    // Even though the event contains a list of alises, the Apple Event Manager
    // will convert each alias to an FSSpec if we ask it to.
    for ( index = 1; index <= numFiles; index ++ )
    {
        err = AEGetNthPtr ( &fileListDesc, index, typeFSS, &actualKeyword,
                            &actualType, (Ptr) &oneFile, sizeof ( oneFile ), &actualSize );
                            
        #if DEBUGGING
        if ( err )
            DebugStrNum ( "\p AEGetNthPtr returned ", err );
        #endif
        
        if ( err )
            break;
        
        err = OpenDocument ( &oneFile, nil );
        
        #if DEBUGGING
        if ( err )
            DebugStrNum ( "\p OpenDocument returned ", err );
        #endif
        
        if ( err )
            break;
    }
    
    
    // Since we want to free all the storage this routine allocates,
    // we'll continue execution here.
    
CleanupAndBail:
    
    AEDisposeDesc ( &fileListDesc );
    return err;
    
} // HandleOdoc
 
 
 
pascal OSErr HandlePdoc ( AEDescList* aevt, AEDescList* reply, long refCon )
{
    #pragma unused(aevt,reply,refCon)
    return errAEEventNotHandled;
}
 
 
 
pascal OSErr HandleQuit ( AEDescList* aevt, AEDescList* reply, long refCon )
{
    #pragma unused(aevt,reply,refCon)
    gQuit = true;
    
    return noErr;
}