home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Utilities / MemoryHelperƒ / MemoryHelper.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-06  |  4.2 KB  |  156 lines  |  [TEXT/ntxt]

  1. #include <AppleEvents.h>
  2. #include <Processes.h>
  3. #include <Aliases.h>
  4.  
  5. // Constants for dealing with FinderEvents. See Chapter 8 of the Apple Event
  6. // Registry for more information.
  7.  
  8. #define kFinderSig    'FNDR'
  9. #define kAEFinderEvents    'FNDR'
  10. #define kSystemType    'MACS'
  11. #define    MOVE_TO_FRONT        -1L
  12. #define    NIL_POINTER            0L
  13. #define    d_Shred            300
  14.  
  15. #define kAEOpenSelection    'sope'
  16. #define keySelection    'fsel'
  17.  
  18. // Prototypes
  19.  
  20. void InitToolbox(void);
  21. OSErr FindAProcess(OSType, OSType, ProcessSerialNumber*);
  22. OSErr OpenSelection(FSSpecPtr theDoc);
  23.  
  24.  
  25. // Given a FSSpecPtr to either an application or a document, OpenSelection creates a
  26. // finder Open Selection Apple event for the object described by the FSSpec.
  27.  
  28. OSErr OpenSelection(FSSpecPtr theDoc)
  29. {
  30.     AppleEvent    aeEvent;    // the event to create;
  31.     AEDesc    myAddressDesc;    // descriptors for the 
  32.     AEDesc    aeDirDesc;
  33.     AEDesc    listElem;
  34.     AEDesc    fileList;        // our list
  35.     FSSpec    dirSpec;
  36.     AliasHandle    dirAlias;        // alias to directory with our file
  37.     AliasHandle    fileAlias;        // alias of the file itself
  38.     ProcessSerialNumber process;    // the finder's psn
  39.     OSErr        myErr;                // duh
  40.     
  41.     // Get the psn of the Finder and create the target address for the .
  42.     if(FindAProcess(kFinderSig,kSystemType,&process))
  43.             return procNotFound;
  44.     myErr = AECreateDesc(typeProcessSerialNumber,(Ptr) &process,
  45.                             sizeof(process), &myAddressDesc);
  46.     if(myErr)    return myErr;
  47.  
  48.     // Create an empty 
  49.     myErr = AECreateAppleEvent (kAEFinderEvents, 'sinf',
  50.                     &myAddressDesc, kAutoGenerateReturnID, kAnyTransactionID,     
  51. &aeEvent);
  52.     if(myErr)
  53.             return myErr;
  54.  
  55.     // Make an FSSpec and alias for the parent folder, and an alias for the file
  56.     FSMakeFSSpec(theDoc->vRefNum,theDoc->parID,nil,&dirSpec);
  57.     NewAlias(nil,&dirSpec,&dirAlias);
  58.     NewAlias(nil,theDoc,&fileAlias);
  59.  
  60.     // Create the file list.
  61.     if(myErr=AECreateList(nil,0,false,&fileList))
  62.             return myErr;
  63.  
  64.     /* Create the folder descriptor
  65.     */
  66.     HLock((Handle)dirAlias);
  67.     AECreateDesc(typeAlias, (Ptr) *dirAlias, GetHandleSize
  68.                     ((Handle) dirAlias), &aeDirDesc);
  69.     HUnlock((Handle)dirAlias);
  70.     DisposHandle((Handle)dirAlias);
  71.  
  72.     if((myErr = AEPutParamDesc(&aeEvent,keyDirectObject,&aeDirDesc)) ==
  73.             noErr)
  74.     {
  75.             AEDisposeDesc(&aeDirDesc);
  76.             HLock((Handle)fileAlias);
  77.  
  78.             AECreateDesc(typeAlias, (Ptr)*fileAlias,
  79.                     GetHandleSize((Handle)fileAlias), &listElem);
  80.             HUnlock((Handle)fileAlias);
  81.             DisposHandle((Handle)fileAlias);
  82.             myErr = AEPutDesc(&fileList,0,&listElem);
  83.     }
  84.     if(myErr)
  85.             return myErr;
  86.     AEDisposeDesc(&listElem);
  87.  
  88.     if(myErr = AEPutParamDesc(&aeEvent,keySelection,&fileList))
  89.             return myErr;
  90.  
  91.     myErr = AEDisposeDesc(&fileList);
  92.  
  93.     myErr = AESend(&aeEvent, nil,
  94.             kAENoReply+kAEAlwaysInteract+kAECanSwitchLayer,
  95.             kAENormalPriority, kAEDefaultTimeout, nil, nil);
  96.     AEDisposeDesc(&aeEvent);
  97. }
  98.  
  99. // Search through the current process list to find the given application. See
  100. // Using the Process Manager for a similar way of doing this.
  101.  
  102. OSErr FindAProcess(OSType typeToFind, OSType creatorToFind,
  103.             ProcessSerialNumberPtr processSN)
  104. {
  105.     ProcessInfoRec    tempInfo;
  106.     FSSpec    procSpec;
  107.     Str31        processName;
  108.     OSErr        myErr = noErr;
  109.  
  110.     // start at the beginning of the process list
  111.     processSN->lowLongOfPSN = kNoProcess;
  112.     processSN->highLongOfPSN = kNoProcess;
  113.  
  114.     // initialize the process information record
  115.     tempInfo.processInfoLength = sizeof(ProcessInfoRec);
  116.     tempInfo.processName = (StringPtr)&processName;
  117.     tempInfo.processAppSpec = &procSpec;
  118.  
  119.     while((tempInfo.processSignature != creatorToFind ||
  120.             tempInfo.processType != typeToFind) ||
  121.             myErr != noErr)
  122.     {
  123.             myErr = GetNextProcess(processSN);
  124.             if (myErr == noErr)
  125.                     GetProcessInformation(processSN, &tempInfo);
  126.     }
  127.     return(myErr);
  128. }
  129.  
  130.  
  131.  
  132.  
  133. DoGet ()
  134. {
  135. char                *c;
  136. Boolean                IsFolder, wasAliased;
  137. OSErr                err;
  138. StandardFileReply    theReply;
  139.  
  140. StandardGetFile    (nil,                   /* address of custom filter? */
  141.                 -1,                /* only looking for one file type */
  142.                 0L,                 /* start of list of file types */
  143.                 &theReply);              /* put results here */
  144.             
  145.     if ( theReply.sfGood == TRUE )
  146.      {
  147.     
  148.         err = ResolveAliasFile(    &(theReply.sfFile),    /* spec to resolve */
  149.                         true,                    /* resolveAliasChains? */
  150.                         &IsFolder,
  151.                         &wasAliased);
  152.         OpenSelection(&(theReply.sfFile));
  153.     }
  154.     ExitToShell();
  155. }
  156.