home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Interim Executive Decision / Outline Table Demo / sources / SendFinderOpen.c < prev    next >
Encoding:
Text File  |  1998-06-21  |  3.7 KB  |  164 lines  |  [TEXT/CWIE]

  1. // File Object Handler Utilities for the WASTE Text Engine
  2. // Part of the WASTE Object Handler Library by Michael Kamprath, kamprath@kagi.com
  3. // maintenance by John C. Daub
  4. //
  5. // v1.2 28 March 1996,  Cleaned up the file with precompiler directives, removing potential
  6. //                        warning messages
  7.  
  8. // Adapted slightly for use in the PowerPlant "Outline Table Demo" application - jcd
  9.  
  10. #ifndef __ALIASES__
  11. #include <Aliases.h>
  12. #endif
  13.  
  14. #ifndef __APPLEEVENTS__
  15. #include <AppleEvents.h>
  16. #endif
  17.  
  18. #include "SendFinderOpen.h"
  19.  
  20. #define kFinderSig            'FNDR'
  21. #define kAEFinderEvents        'FNDR'
  22. #define kSystemType            'MACS'
  23.  
  24. #define    kAEOpenSelection    'sope'
  25. #define keySelection        'fsel'
  26.  
  27. #ifndef true
  28. #define true 1
  29. #endif
  30.  
  31. #ifndef false
  32. #define false 0
  33. #endif
  34.  
  35. OSErr SendFinderOpenAE(FSSpec *theDoc)
  36. {
  37. AppleEvent            aeEvent;
  38. AEDesc                myAddressDesc;
  39. AEDesc                aeDirDesc;
  40. AEDesc                listElem;
  41. AEDescList            fileList;
  42. FSSpec                dirSpec;
  43. AliasHandle            dirAlias;
  44. AliasHandle            fileAlias;
  45. ProcessSerialNumber    process;
  46. OSErr                myErr;
  47. OSType            FndrType = 'MACS';
  48.     
  49.     /*
  50.      * Get the psn of the Finder and create the target address for the AE
  51.      */
  52.     
  53.     if ( FindAProcess( kFinderSig, kSystemType, &process) )
  54.         return( procNotFound );
  55.  
  56.     myErr = AECreateDesc( typeProcessSerialNumber, (Ptr)&process, 
  57.                             sizeof(process), &myAddressDesc);
  58.     if (myErr) return(myErr);
  59.     
  60.     /*
  61.      * Create an empty Apple Event
  62.      */
  63.     
  64.     myErr = AECreateAppleEvent( kAEFinderEvents, kAEOpenSelection, &myAddressDesc,
  65.                                 kAutoGenerateReturnID, kAnyTransactionID, &aeEvent);
  66.     
  67.     if (myErr) return(myErr);
  68.     
  69.     /*
  70.      * Make and FSSpec for the parent folder and an alias of the file.
  71.      */
  72.     
  73.     FSMakeFSSpec( theDoc->vRefNum, theDoc->parID, 0L, &dirSpec);
  74.     NewAlias( nil, &dirSpec, &dirAlias);
  75.     NewAlias( nil, theDoc, &fileAlias);
  76.     
  77.     
  78.     /*
  79.      * Create the File list.
  80.      *
  81.      */
  82.     
  83.     HLockHi( (Handle)dirAlias);
  84.     AECreateDesc( typeAlias, (Ptr)*dirAlias, GetHandleSize( (Handle)dirAlias), &aeDirDesc);
  85.     HUnlock( (Handle)dirAlias);
  86.     DisposeHandle( (Handle)dirAlias);
  87.     
  88.     if ( (myErr=AEPutParamDesc(&aeEvent, keyDirectObject, &aeDirDesc)) == noErr )
  89.     {
  90.         AEDisposeDesc( &aeDirDesc );
  91.         
  92.         AECreateList(nil, 0, false, &fileList);
  93.         HLockHi( (Handle)fileAlias);
  94.         myErr = AECreateDesc( typeAlias, (Ptr)*fileAlias, 
  95.                                     GetHandleSize( (Handle)fileAlias), &listElem);
  96.         HUnlock( (Handle)fileAlias);
  97.  
  98.         DisposeHandle( (Handle)fileAlias);
  99.     
  100.         myErr = AEPutDesc( &fileList, 0L, &listElem);
  101.         
  102.         if (myErr) return(myErr);
  103.         
  104.     }
  105.     if (myErr) return(myErr);
  106.     
  107.     AEDisposeDesc(&listElem);
  108.     
  109.     myErr = AEPutParamDesc(&aeEvent, keySelection, &fileList);
  110.     if (myErr) return(myErr);
  111.  
  112.     myErr = AEDisposeDesc( &fileList);
  113.     if (myErr) return(myErr);
  114.     
  115.     myErr = AESend( &aeEvent, 0L, kAENoReply+kAEAlwaysInteract+kAECanSwitchLayer,
  116.                     kAENormalPriority, kAEDefaultTimeout, 0L, 0L);\
  117.                 
  118.     AEDisposeDesc(&aeEvent);
  119.     
  120.     myErr = SetFrontProcess( &process );
  121.     
  122.     return(myErr);
  123. }
  124.  
  125. /*
  126.  * Search though the current process list to find the given application. 
  127.  *
  128.  */
  129.  
  130. OSErr FindAProcess( OSType typeToFind, OSType creatorToFind, ProcessSerialNumberPtr processSN)
  131. {
  132. ProcessInfoRec            tempInfo;
  133. FSSpec                    procSpec;
  134. Str31                    processName;
  135. OSErr                    myErr = noErr;
  136.  
  137.     /*
  138.      * Start at begining of process list
  139.      */
  140.     
  141.     processSN->lowLongOfPSN = kNoProcess;
  142.     processSN->highLongOfPSN = kNoProcess;
  143.     
  144.     /*
  145.      * Init the process information record.
  146.      *
  147.      */
  148.     
  149.     tempInfo.processInfoLength = sizeof(ProcessInfoRec);
  150.     tempInfo.processName = (StringPtr)&processName;
  151.     tempInfo.processAppSpec = &procSpec;
  152.     
  153.     while ( (tempInfo.processSignature != creatorToFind || tempInfo.processType != typeToFind)
  154.                 || myErr != noErr)
  155.     {
  156.         myErr = GetNextProcess(processSN);
  157.         if (myErr == noErr)
  158.             GetProcessInformation( processSN, &tempInfo);
  159.     }
  160.         
  161.     return(myErr);
  162. }
  163.  
  164.