odoc.c

/*
    File:       odoc.c
 
    Contains:   This sample sends an 'odoc' event to a specified running application
 
    Written by: Steven Falkenburg   
 
    Copyright:  Copyright © 1991-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):
                7/21/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                11/16/92                    fixed bug in blockmoving target ID name 
                                            (was moving too much data)
*/
#include <Types.h>
#include <PPCToolBox.h>
#include <AppleEvents.h>
#include <Aliases.h>
#include <EPPC.h>
#include <Fonts.h>
#include <Windows.h>
#include <Dialogs.h>
#include <Menus.h>
#include <Fonts.h>
#include <StandardFile.h>
 
 
void main(void);
void InitStuff(void);
OSErr GetTargetAddress(Str255 prompt,Str255 appStr,PortInfoRec *myPortInfo,
                        AEAddressDesc *targetAddress,TargetID *toTargetID);
OSErr SendODOC(AEAddressDesc *targetAddress,AliasHandle targetFile);
Boolean GetFileToOpen(FSSpec *target);
 
void main(void)
{
    PortInfoRec portInfo;
    AEAddressDesc targetAddress;
    TargetID targetID;
    OSErr err;
    FSSpec fileSpec;
    AliasHandle aliasTarget;
    
    InitStuff();
    
    if (!GetFileToOpen(&fileSpec))
        ExitToShell();
    
    if (GetTargetAddress("\pSpecify target of ODOC:",nil,&portInfo,&targetAddress,&targetID)==noErr) {
        err = NewAlias(nil,&fileSpec,&aliasTarget);
        if (err==noErr) {
            err = SendODOC(&targetAddress,aliasTarget);
            err = AEDisposeDesc(&targetAddress);
            DisposeHandle((Handle)aliasTarget);
        }
    }
}
 
 
void InitStuff(void)
{
    InitGraf(&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(nil);
    InitCursor();
    FlushEvents(everyEvent,0);
}
 
 
Boolean GetFileToOpen(FSSpec *target)
{
    StandardFileReply sfReply;
    
    StandardGetFile(nil,-1,nil,&sfReply);
    BlockMove(&sfReply.sfFile,target,sizeof(FSSpec));
    
    return sfReply.sfGood;
}
 
 
OSErr GetTargetAddress(Str255 prompt,Str255 appStr,PortInfoRec *myPortInfo,
                        AEAddressDesc *targetAddress,TargetID *toTargetID)
{
    OSErr err;
    
    err = PPCBrowser(prompt,appStr,false,&toTargetID->location,myPortInfo,nil,"\p");
    if (err==noErr) {
        toTargetID->name = myPortInfo->name;
        err = AECreateDesc(typeTargetID,(Ptr)toTargetID,sizeof(TargetID),targetAddress);
    }
    
    return err;
}
 
 
OSErr SendODOC(AEAddressDesc *targetAddress,AliasHandle targetFile)
{
    OSErr err;
    AppleEvent theAE,reply;
    AEDescList descList;
    
    reply.dataHandle = nil;
    
    err = AECreateAppleEvent(kCoreEventClass,kAEOpenDocuments,targetAddress,
                kAutoGenerateReturnID,kAnyTransactionID,&theAE);
    if (err!=noErr)
        return err;
    
    err = AECreateList(nil,0,false,&descList);
    if (err!=noErr)
        return err;
    
    HLock((Handle)targetFile);
    err = AEPutPtr(&descList,0,typeAlias,(Ptr)*targetFile,sizeof(AliasHandle)+(**targetFile).aliasSize);
    HUnlock((Handle)targetFile);
    if (err!=noErr)
        return err;
    
    err = AEPutParamDesc(&theAE,keyDirectObject,&descList);
    if (err!=noErr)
        return err;
    
    err = AESend(&theAE,&reply,kAENoReply,kAENormalPriority,kAEDefaultTimeout,nil,nil);
    
    err = AEDisposeDesc(&descList);
    err = AEDisposeDesc(&theAE);
    if (reply.dataHandle)
        err = AEDisposeDesc(&reply);
    return err;
}