MoreAppleEvents/MoreAEObjects.c

/*
    File:       MoreAEObjects.c
 
    Contains:   Functions to help you when you are working with Apple event objects.
 
    Written by: George Warner
 
    Copyright:  Copyright (c) 2000 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):
 
         <1>      3/9/00    GW      Intergrating AppleEvent Helper code. First Check In
*/
 
//*******************************************************************************
//  Conditionals to setup the build environment the way we like it.
#include "MoreSetup.h"
//**********    Universal Headers       ****************************************
#include <AERegistry.h>
#include <AEObjects.h>
#include <AEPackObject.h>
#include <Aliases.h>
#include <FinderRegistry.h>
#include <Icons.h>
#include <Processes.h>
//**********    Project Headers         ****************************************
#include "MoreAppleEvents.h"
#include "MoreAEObjects.h"
//**********    Private Prototypes      ****************************************
/*
    Used by MoreFECreateIconFamilyRecord, passed to ForEachIconDo as the IconAction
    function. Puts each icon in an icon suite into the descriptor record passed
    in the myDataPtr parameter.
*/
 
static  pascal  OSErr   MyIconAction( ResType pIconType,
                              const Handle *pIconHdl,
                              void *pDataPtr);
/********************************************************************************
    Add a parameter of type typeAlias to an AERecord (or AppleEvent) using the provided FSSpec.
 
    pFSSpec         input:  Pointer to the FSSpec to use.
    pKeyword        input:  The key for the data to be added to the record.
    pAERecord       input:  Pointer to the record (or event) to add the data to.
    
    RESULT CODES
    ____________
    noErr              0    No error    
    paramErr         -50    The value of target or alias parameter, or of
                            both, is NIL, or the alias record is corrupt
    memFullErr      -108    Not enough room in heap zone    
*/
pascal  OSErr   MoreAEOAddAliasParameterFromFSSpec(const FSSpecPtr pFSSpec,
                                                const DescType pKeyword,
                                                AERecord *pAERecord )
{
    OSErr           anErr = noErr;
    AliasHandle     aliasHandle;
    
    anErr = NewAlias( nil, pFSSpec, &aliasHandle);
    if ( anErr == noErr  &&  aliasHandle == nil )
    {
        anErr = paramErr;
    }
    
    if ( anErr == noErr )
    {
        char    handleState;
        
        handleState = HGetState( (Handle)aliasHandle );
        HLock( (Handle)aliasHandle );
        
        anErr = AEPutParamPtr( pAERecord, pKeyword, typeAlias,
                               *aliasHandle, (*aliasHandle)->aliasSize);
        
        HSetState( (Handle)aliasHandle, handleState );
        DisposeHandle( (Handle)aliasHandle );
    }
        
    return anErr;
}//end AddAliasParameterFromFSS
/********************************************************************************
    Create and return an AEDesc of type typeAlias using the provided FSSpec.
 
    pFSSpec         input:  Pointer to the FSSpec to use.
    pAliasAEDesc    input:  Pointer to null AEDesc.
                    output: an AEDesc of type typeAlias.
    
    RESULT CODES
    ____________
    noErr              0    No error    
    paramErr         -50    The value of target or alias parameter, or of
                            both, is NIL, or the alias record is corrupt
    memFullErr      -108    Not enough room in heap zone    
*/
pascal  OSErr   MoreAEOCreateAliasDescFromFSSpec( const FSSpecPtr pFSSpec,
                                                  AEDesc *pAliasAEDesc )
{
    OSErr           anErr = noErr;
    AliasHandle     aliasHandle;
    
    anErr = NewAlias( nil, pFSSpec, &aliasHandle);
    if ( anErr == noErr  &&  aliasHandle == nil )
    {
        anErr = paramErr;
    }
 
    if ( anErr == noErr )
    {
        anErr = MoreAEOCreateAliasDesc( aliasHandle, pAliasAEDesc );
        DisposeHandle( (Handle)aliasHandle );
    }
        
    return anErr;
}//end MakeAliasObject
/********************************************************************************
    Create and return an AEDesc of type typeAlias using the provided 
    alias record.
 
    pAliasHdl       input:  Handle to an alias record.
    pAliasAEDesc    input:  Pointer to null AEDesc.
                    output: an AEDesc of type typeAlias.
    
    RESULT CODES
    ____________
    noErr              0    No error    
    memFullErr      -108    Not enough room in heap zone    
*/
pascal  OSErr   MoreAEOCreateAliasDesc( const AliasHandle pAliasHdl,
                                        AEDesc *pAliasAEDesc )
{
    OSErr   anErr = noErr;
    
    char    handleState = HGetState( (Handle)pAliasHdl );
    HLock( (Handle)pAliasHdl );
    
    anErr = AECreateDesc( typeAlias, *pAliasHdl, GetHandleSize( (Handle)pAliasHdl ), pAliasAEDesc );
    
    HSetState( (Handle)pAliasHdl, handleState );
    
    return anErr;
}//end MakeAliasObject
/********************************************************************************
    Given an FSSpec, return an object descriptor containing an alias,
    contained by containerObj.
    
    pFSSpec             input:  Pointer to the FSSpec to use.
    pContainerAEDesc    input:  Pointer to container object for object being created.
    pAliasObjectAEDesc      input:  Pointer to null AEDesc.
                        output: an alias object.
    
    RESULT CODES
    ____________
    noErr                   0   No error    
    paramErr              -50   The value of target or alias parameter, or of
                                both, is NIL, or the alias record is corrupt
    memFullErr           -108   Not enough room in heap zone    
    errAECoercionFail   -1700   Data could not be coerced to the requested 
                                Apple event data type   
    errAEWrongDataType  -1703   Wrong Apple event data type 
    errAENotAEDesc      -1704   Not a valid descriptor record   
    errAEBadListItem    -1705   Operation involving a list item failed  
*/
pascal  OSErr   MoreAEOCreateAliasObjectFromFSSpec( const FSSpecPtr pFSSpec,
                                                    AEDesc *pContainerAEDesc,
                                                    AEDesc *pAliasObjectAEDesc )
{
    OSErr       anErr = noErr;
    AEDesc      aliasDesc = {typeNull, nil};
 
    anErr = MoreAEOCreateAliasDescFromFSSpec( pFSSpec, &aliasDesc );
    if ( anErr == noErr )
    {
        anErr = CreateObjSpecifier( typeAlias, pContainerAEDesc, formAbsolutePosition,
                                    &aliasDesc, false, pAliasObjectAEDesc );
        AEDisposeDesc( &aliasDesc );
    }
    
    return anErr;
}//end MakeAliasObject
/********************************************************************************
    Given an AliasHandle, return an object descriptor containing an alias,
    contained by containerObj.
    
    pAliasHdl       input:  Handle to an alias record.
    pContainerAEDesc    input:  Pointer to container object for object being created.
    pAliasObjectAEDesc  input:  Pointer to null AEDesc.
                    output: an alias object.
    
    RESULT CODES
    ____________
    noErr                   0   No error    
    paramErr              -50   Error in parameter list
    memFullErr           -108   Not enough room in heap zone    
    errAECoercionFail   -1700   Data could not be coerced to the requested 
                                Apple event data type   
    errAEWrongDataType  -1703   Wrong Apple event data type 
    errAENotAEDesc      -1704   Not a valid descriptor record   
    errAEBadListItem    -1705   Operation involving a list item failed  
*/
pascal  OSErr   MoreAEOCreateAliasObject( const AliasHandle pAliasHdl,
                                          AEDesc *pContainerAEDesc,
                                          AEDesc *pAliasObjectAEDesc )
{
    OSErr   anErr = noErr;
    AEDesc  aliasDesc;
 
    anErr = MoreAEOCreateAliasDesc( pAliasHdl, &aliasDesc );
    if ( anErr == noErr )
    {
        anErr = CreateObjSpecifier( typeAlias, pContainerAEDesc, formAbsolutePosition,
                                    &aliasDesc, false, pAliasObjectAEDesc );
        AEDisposeDesc( &aliasDesc );
    }
    
    return anErr;
}//end MakeAliasObject
/********************************************************************************
    Given a property type, create an new object descriptor for that property,
    contained by containerObj.
    
    pPropertyType       input:  Property type to use for object.
    pContainerAEDesc    input:  Pointer to container object for object being created.
    propertyObjPtr  input:  Pointer to null AEDesc.
                    output: A property object.
    
    RESULT CODES
    ____________
    noErr                   0   No error    
    paramErr              -50   Error in parameter list
    memFullErr           -108   Not enough room in heap zone    
    errAECoercionFail   -1700   Data could not be coerced to the requested 
                                Apple event data type   
    errAEWrongDataType  -1703   Wrong Apple event data type 
    errAENotAEDesc      -1704   Not a valid descriptor record   
    errAEBadListItem    -1705   Operation involving a list item failed  
*/
pascal  OSErr   MoreAEOCreatePropertyObject( const DescType pPropertyType,
                                             AEDesc *pContainerAEDesc,
                                             AEDesc *propertyObjPtr )
{
    OSErr   anErr = noErr;
    AEDesc  propDesc;
    
    anErr = AECreateDesc( typeType, &pPropertyType, sizeof( pPropertyType ), &propDesc );
    if ( anErr == noErr )
    {
        anErr = CreateObjSpecifier( cProperty, pContainerAEDesc, formPropertyID,
                                    &propDesc, false, propertyObjPtr );
        AEDisposeDesc( &propDesc );
    }
    
    return anErr;
}//end MakePropertyObject
/********************************************************************************
    Given a ProcessSerialNumber, create an new object descriptor for the PSN,
    contained by containerObj.
    
    pPSN            input:  ProcessSerialNumber to use for object.
    pContainerAEDesc    input:  Pointer to container object for object being created.
    pPSNObjDesc     input:  Pointer to null AEDesc.
                    output: A ProcessSerialNumber object.
    
    RESULT CODES
    ____________
    noErr                   0   No error    
    paramErr              -50   Error in parameter list
    memFullErr           -108   Not enough room in heap zone    
    errAECoercionFail   -1700   Data could not be coerced to the requested 
                                Apple event data type   
    errAEWrongDataType  -1703   Wrong Apple event data type 
    errAENotAEDesc      -1704   Not a valid descriptor record   
    errAEBadListItem    -1705   Operation involving a list item failed  
*/
pascal  OSErr   MoreAEOCreateProcessObject( const ProcessSerialNumber *pPSN,
                                            AEDesc *pContainerAEDesc,
                                            AEDesc *pPSNObjDesc )
{
    OSErr   anErr = noErr;
    AEDesc  psnDesc;
    
    anErr = AECreateDesc( typeProcessSerialNumber, pPSN, sizeof( ProcessSerialNumber ), &psnDesc );
    if ( anErr == noErr )
    {
        anErr = CreateObjSpecifier( cProperty, pContainerAEDesc, formPropertyID,
                                    &psnDesc, false, pPSNObjDesc );
        AEDisposeDesc( &psnDesc );
    }
    
    return anErr;
}//end MakePropertyObject
/********************************************************************************
    Given selection type, create an new object descriptor for a selection,
    contained by containerObj.
    
    pSelectionAEDesc    input:  Selection type to use for object.
    pContainerAEDesc    input:  Pointer to container object for object being created.
    pSelectionObject        input:  Pointer to null AEDesc.
                        output: A property object.
    
    RESULT CODES
    ____________
    noErr                   0   No error    
    paramErr              -50   Error in parameter list
    memFullErr           -108   Not enough room in heap zone    
    errAECoercionFail   -1700   Data could not be coerced to the requested 
                                Apple event data type   
    errAEWrongDataType  -1703   Wrong Apple event data type 
    errAENotAEDesc      -1704   Not a valid descriptor record   
    errAEBadListItem    -1705   Operation involving a list item failed  
*/
pascal  OSErr   MoreAEOCreateSelectionObject( const DescType pSelectionAEDesc,
                                              AEDesc *pContainerAEDesc,
                                              AEDesc *pSelectionObject )
{
    OSErr   anErr = noErr;
    
    AEDesc  selectionDesc = {typeNull, nil};
    
    anErr = AECreateDesc( typeAbsoluteOrdinal, &pSelectionAEDesc, sizeof( pSelectionAEDesc ), &selectionDesc );
    if ( anErr == noErr )
    {
        anErr = CreateObjSpecifier( cObject, pContainerAEDesc, formAbsolutePosition,
                                    &selectionDesc, false, pSelectionObject );
        AEDisposeDesc( &selectionDesc );
    }           
    return anErr;
}
/********************************************************************************
    Make position list (a list containing two longs representin the x and y values
    for the position of a Finder item).
    
    pPosition           input:  A point specifying the position.
    pPositionAEList     input:  Pointer to an AEList (contents will be lost, but not disposed).
                        output: A new AEList containing the x & y values for the position.
    
    Result Codes
    ____________
    noErr                   0   No error    
    memFullErr           -108   Not enough room in heap zone    
*/
pascal  OSErr   MoreAEOCreatePositionList( const Point pPosition,
                                     AEDescList * pPositionAEList )
{
    OSErr   anErr = noErr;
    
    anErr = AECreateList( nil, 0, false, pPositionAEList );
    if ( anErr == noErr )
    {
        anErr = AEPutPtr( pPositionAEList, 0, typeInteger, &(pPosition.h), sizeof( short ) );
        if ( anErr == noErr )
        {
            anErr = AEPutPtr( pPositionAEList, 0, typeInteger, &(pPosition.v), sizeof( short ) );
        }
    }
    
    return anErr;
}//end MoreAEOCreatePositionList
 
//********************************************************************************
// A simple wrapper around CreateObjSpecifier which creates
// an object specifier using formUniqueID and the unique ID
// in pKeyData.
pascal OSStatus MoreAEOCreateObjSpecifierFormUniqueID(DescType pDesiredClass, const AEDesc *pContainer, 
                                                SInt32 pKeyData, Boolean pDisposeInputs, 
                                                AEDesc *pObjSpecifier)
{
    OSStatus err;
    AEDesc   keyDesc;
 
    MoreAssertQ(pContainer != nil);
    MoreAssertQ(pObjSpecifier != nil);
 
    MoreAENullDesc(&keyDesc);
    
    err = AECreateDesc(typeLongInteger, &pKeyData, sizeof(SInt32), &keyDesc);
    if (err == noErr) {
        err = CreateObjSpecifier(pDesiredClass, (AEDesc *) pContainer, formUniqueID, &keyDesc, pDisposeInputs, pObjSpecifier);
    }
    
    MoreAEDisposeDesc(&keyDesc);
    
    return err;
}
 
//********************************************************************************
// A simple wrapper around CreateObjSpecifier which creates
// an object specifier using formAbsolutePosition, a key of
// typeLongInteger (rather than typeAbsoluteOrdinal) and the
// position index in pKeyData.
pascal OSStatus MoreAEOCreateObjSpecifierFormAbsPos(DescType pDesiredClass, const AEDesc *pContainer, 
                                            SInt32 pKeyData, SInt32 pDisposeInputs, 
                                            AEDesc *pObjSpecifier)
{
    OSStatus err;
    AEDesc   keyDesc;
 
    MoreAssertQ(pContainer != nil);
    MoreAssertQ(pObjSpecifier != nil);
 
    MoreAENullDesc(&keyDesc);
    
    err = AECreateDesc(typeLongInteger, &pKeyData, sizeof(SInt32), &keyDesc);
    if (err == noErr) {
        err = CreateObjSpecifier(pDesiredClass, (AEDesc *) pContainer, formAbsolutePosition, &keyDesc, pDisposeInputs, pObjSpecifier);
    }
    
    MoreAEDisposeDesc(&keyDesc);
    
    return err;
}
 
//********************************************************************************
// A simple wrapper around CreateObjSpecifier which creates
// an object specifier using formName and the name in pKeyData.
pascal OSStatus MoreAEOCreateObjSpecifierFormName(DescType pDesiredClass, const AEDesc *pContainer, 
                                            ConstStr255Param pKeyData, Boolean pDisposeInputs, 
                                            AEDesc *pObjSpecifier)
{
    OSStatus err;
    AEDesc   keyDesc;
 
    MoreAssertQ(pContainer != nil);
    MoreAssertQ(pKeyData != nil);
    MoreAssertQ(pObjSpecifier != nil);
    
    MoreAENullDesc(&keyDesc);
    
    err = AECreateDesc(typeText, &pKeyData[1], pKeyData[0], &keyDesc);
    if (err == noErr) {
        err = CreateObjSpecifier(pDesiredClass, (AEDesc *) pContainer, formName, &keyDesc, pDisposeInputs, pObjSpecifier);
    }
    MoreAEDisposeDesc(&keyDesc);
    return err;
}
 
//********************************************************************************
// *******************************************
// *****       AddTwoIntListToEvent      *****
// *******************************************
//  Creates an AEList containing 2 integers, and then adds the list to an Apple event.
 
/*
OSErr AddTwoIntListToEvent( AppleEvent *pAppleEvent, long param1, long param2 )
{
    OSErr   anErr;
 
    AEDescList  paramList;
    anErr = AECreateList( nil, 0, false, &paramList );
    if ( anErr == noErr )
    {
        anErr = AEPutPtr( &paramList, 0, typeInteger, &param1, sizeof( param1 ) );
        if ( anErr == noErr )
        {
            anErr = AEPutPtr( &paramList, 0, typeInteger, &param2, sizeof( param2 ) );
            if ( anErr == noErr )
            {
                anErr = AEPutParamDesc( pAppleEvent, keyDirectObject, &paramList );
            }
        }
    }
    
    AEDisposeDesc( &paramList );
    return ( anErr );
}// end AddTwoIntListToEvent
 
*/