FSSetPrinter.c

/********************************************************************************
    FSSetPrinter.c
    
    This file does the deed of constructing the AppleEvent that tells the 
    finder which desktop printer to select as the new default printer.
    
    ©1995  Apple Computer, Inc.
    All rights reserved.
 
********************************************************************************/
 
 
#include <AppleEvents.h>
#include <PrintingMessages.h>
#include <GXExceptions.h>
 
#include "FSSetPrinter.h"
 
 
// ----------------------------------------------------------------------
/*
    Create the Finder address we wish to send to - note that we mean here "Finder" in the
    most generic sense.  In reality, this can be the Finder or AtEase.
*/
 
static OSErr GetFinderAddr(AEAddressDesc *theDesc)
{
    OSErr               result = noErr;
    ProcessInfoRec      processInfo;
    ProcessSerialNumber serialNumber;
    
 
    serialNumber.highLongOfPSN = 0;
    serialNumber.lowLongOfPSN = kNoProcess;
    
    while (result = (GetNextProcess(&serialNumber) == noErr))
        {
        processInfo.processInfoLength = sizeof(ProcessInfoRec);
        processInfo.processName     = nil;
        processInfo.processAppSpec  = nil;
        
        result = GetProcessInformation(&serialNumber, &processInfo);
        if (result == noErr)
            {
            if ( processInfo.processType == 'FNDR' )
                {
                result = AECreateDesc(typeProcessSerialNumber, (Ptr)&serialNumber, sizeof(ProcessSerialNumber), theDesc);
                return(result);
                }
            }
        }
 
    return(result);
    
} // GetFinderAddr
 
 
// ----------------------------------------------------------------------
/*
    Routine to send an AppleEvent to the Finder.
    Note that the data is sub-typed so the Finder can decipher the contents.
*/
 
OSErr   SASendAEToFinder(
    Ptr                 dataPtr,        // -> AppleEvent data
    Size                dataSize)       // size of AppleEvent data
{
    OSErr           result;
    AEAddressDesc   finderAddr;     // the address of the Finder as an AppleEvent Descriptor
    AppleEvent      theEvent;
    AppleEvent      replyEvent;
    
    
    result = GetFinderAddr(&finderAddr);
    nrequire(result, Fail_GetAddr);
    
    result = AECreateAppleEvent(
        kCoreEventClass,            // this is a core event
        kFinderExtension,           // for a Finder extension
        &finderAddr,                // and we send it to the Finder
        kAutoGenerateReturnID,      // we aren't getting a return
        kAnyTransactionID,          // and we don't care about the transaction #
        &theEvent);                 // and we create it right here
    nrequire(result, Fail_MakeEvent);
            
    result = AEPutParamPtr(
        &theEvent,                  // the event to shove into
        keyDirectObject,            // direct object keyword
        kFinderExtension,           // for the Finder extension
        dataPtr,                    // here's the data!
        dataSize);                  // here's how long it is!
    nrequire(result, Fail_StuffParm);
    
    result = AESend(
        &theEvent,                  // send the status event
        &replyEvent,                // no reply event because -
        kAENoReply +                // we don't want a reply
         kAECanInteract +           // the receiver can interact with user
         kAEDontReconnect,          // and don't bother to reconnect on error
        kAENormalPriority,          // just a normal event
        kAEDefaultTimeout,          // we'll wait some reasonable amount of time
        nil, nil);                  // and don't care what happens during that.
 
 
//  Exception Handling and drop through cleanup
 
Fail_StuffParm:
    // now get rid of the AppleEvent
    (void) AEDisposeDesc(&theEvent);
 
Fail_MakeEvent:
    // and get rid of the Finder address
    (void) AEDisposeDesc(&finderAddr);
 
Fail_GetAddr:
 
    // Fall through and return errror, if any
    return(result);
    
} // SASendAEToFinder
 
 
// ----------------------------------------------------------------------
/*
    This function constructs the data block that gets sent to the finder
    via SASendAEToFinder containing the destination of the AppleEvent, which
    event it is (kSetDefaultPrinterType) and the name of the desktop printer.
*/
 
OSErr SendTestAE(StringPtr dtpName)
{
    OSErr           err=noErr;
    SetDTPEvent     myEvent;
    
    myEvent.pfeCreator = kPrintingExtension;            // to finder
    myEvent.extensionType = kSetDefaultPrinterType;     // change default printer
    BlockMove(dtpName,&myEvent.dtpName,dtpName[0]+1);   // copy name of new default printer
        
    err = SASendAEToFinder((Ptr) &myEvent,sizeof(SetDTPEvent));
            
    return (err);
}