home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Pascal / source / open-selection-pas.txt < prev    next >
Encoding:
Internet Message Format  |  1994-03-23  |  6.7 KB  |  [TEXT/EDIT]

  1. From: mxmora@unix.sri.com (Matthew Xavier Mora)
  2. Subject: OpenSelection Code (pascal source) 
  3. Date: 11 Sep 92 17:11:08 GMT 
  4.  
  5.  
  6. Here's the code to have the finder open a control panel.
  7. Just pass the control panel's fsspec to the openselection
  8. function and it should open it. Actually I think it will
  9. open any valid fsspec but I wouldn't bet on it. I haven't
  10. stressed this code so there are probably some bugs in it.
  11.  
  12. Matt
  13.  
  14. --------------------------------------------
  15. { OpenSelection.p ported by Matthew Xavier Mora} 
  16. { ported from C.K. Han's openselection.c code  }
  17. { 09-11-92                                     }
  18.  
  19. unit openSelection; 
  20. interface
  21.   uses
  22.     AppleTalk, PPCToolBox, Processes, EPPC, Notification, AppleEvents,
  23.     {, Errors, Events, Memory, Resources, SegLoad, }
  24.  
  25.     (* Group 3 *)
  26.     Aliases {, thermometer};
  27.  
  28.   function OpenSelection (var theFileToOpen: FSSpec): OSErr;
  29.  
  30. implementation
  31.   const
  32.     kFinderType = 'FNDR';
  33.     kSysCreator = 'MACS';
  34.     kAEOpenSelection = 'sope';
  35.     aeSelectionKeyword = 'fsel';
  36.  
  37. { This runs through the process list looking for the indicated application
  38. }
  39.   function FindAProcess (typeToFind, creatorToFind: Ostype;
  40.                          var processSN:ProcessSerialNumber;
  41.                          var infoRecToFill: ProcessInfoRec): OSErr;
  42.     var
  43.       tempPSN: ProcessSerialNumber;
  44.       myErr: OSErr;
  45.  
  46.   begin
  47.     myErr := noErr;
  48.     tempPSN.lowLongOfPSN := kNoProcess;
  49.     processSN.lowLongOfPSN := kNoProcess;
  50.     processSN.highLongOfPSN := kNoProcess;
  51.     repeat
  52.       begin
  53.         myErr := GetNextProcess(processSN);
  54.         if (myErr = noErr) then
  55.           myErr := GetProcessInformation(processSN, infoRecToFill);
  56.       end;
  57.     until ((infoRecToFill.processSignature = creatorToFind) or
  58.    (infoRecToFill.processType = Longint(typeToFind)) or (myErr <> noErr));
  59.  
  60.     FindAProcess := myErr;
  61.   end;
  62.  
  63.   function OpenSelection (var theFileToOpen: FSSpec): OSErr;
  64.     var
  65.       aeEvent, aeReply: AppleEvent;
  66.       aeDirDesc, listElem: AEDesc;
  67.       interactErr: OSErr;
  68.       dirSpec, procSpec: FSSpec;
  69.       fileList: AEDesc;
  70.       myReply: StandardFileReply;
  71.       myErr: OSErr;
  72.       process: ProcessSerialNumber;
  73.       DirAlias, FileAlias: AliasHandle;
  74.       infoRec: ProcessInfoRec;
  75.       processName: Str31;
  76.       fullPath, appName: Str255;
  77.       myAddressDesc: AEDesc;
  78.   begin
  79.     interactErr := noErr;
  80.  
  81.     if (true) then
  82.       begin
  83.         infoRec.processInfoLength := sizeof(ProcessInfoRec);
  84.         infoRec.processName := @processName;
  85.         infoRec.processAppSpec := @procSpec;
  86.  
  87.         myErr := FindAProcess(kFinderType, kSysCreator, process, infoRec);
  88.         if (myErr = noErr) then
  89.           myErr := AECreateDesc(typeProcessSerialNumber, @process,
  90.                                 sizeof(process), myAddressDesc);
  91.  
  92.  
  93.         if (myErr = noErr) then
  94.           begin
  95.  
  96.             { Create the FinderEvent }
  97.             myErr := AECreateAppleEvent(kFinderType, kAEOpenSelection,
  98.                                         myAddressDesc,
  99. kAutoGenerateReturnID,
  100.                                         kAnyTransactionID, aeEvent);
  101.             { If you want to keep talking to this machine, you can keep
  102. this  }
  103.             { address desc around }
  104.             myErr := AEDisposeDesc(myAddressDesc);
  105.  
  106.             if (myErr = noErr) then
  107.               begin
  108.                 { Now we build all the bits of an OpenSelection event. }
  109.                 { Basically, we need to create an alias for the item to
  110. open,}
  111.                 { and an alias to the parent }
  112.                 { folder (directory) of that item. }
  113.                 { We can also pass a list of files if we want.  }
  114.                 { You'll notice that for opening a finder window, the file
  115. and}
  116.                 { directory alias both point at the }
  117.                 { folder itself }
  118.                 { make a spec for the parent folder }
  119.  
  120.  
  121.                 myErr := FSMakeFSSpec(theFileToOpen.vRefNum,
  122.                                       theFileToOpen.parID, '', dirSpec);
  123.                 myErr := NewAlias(nil, dirSpec, DirAlias);
  124.  
  125.                 { Create alias for file }
  126.                 { if you are opening a window, then you make the file alias
  127. the}
  128.                 { same as the dir alias }
  129.                 myErr := NewAlias(nil, theFileToOpen, FileAlias);
  130.                 interactErr := AEInteractWithUser(kAEDefaultTimeout, nil,
  131. nil);
  132.                 if (interactErr = noErr) then
  133.                   myErr := SetFrontProcess(process);
  134.  
  135.                 { Create the file  list }
  136.                 if (myErr = noErr) then
  137.                   begin
  138.                     myErr := AECreateList(nil, 0, false, fileList);
  139.  
  140.                     {  create the folder  descriptor }
  141.                     HLock(Handle(DirAlias));
  142.                     myErr := AECreateDesc(typeAlias, Ptr(DirAlias^),
  143.                                           GetHandleSize(Handle(DirAlias)),
  144.                                           aeDirDesc);
  145.                     HUnlock(Handle(DirAlias));
  146.                     myErr := AEPutParamDesc(aeEvent, keyDirectObject,
  147.                                             aeDirDesc);
  148.                     if (myErr = noErr) then
  149.                       begin
  150.                         { done with the desc, kill it }
  151.                         myErr := AEDisposeDesc(aeDirDesc);
  152.                         {  create the file descriptor and add to aliasList
  153. }
  154.                         HLock(Handle(FileAlias));
  155.                         myErr := AECreateDesc(typeAlias, Ptr(FileAlias^),
  156.                                              
  157. GetHandleSize(Handle(FileAlias)),
  158.                                               listElem);
  159.                         HLock(Handle(FileAlias));
  160.                         myErr := AEPutDesc(fileList, 0, listElem);
  161.                       end;
  162.                     if (myErr = noErr) then
  163.                       begin
  164.  
  165.                         myErr := AEDisposeDesc(listElem);
  166.  
  167.                         { Add the file alias list to the event }
  168.                         myErr := AEPutParamDesc(aeEvent,
  169. aeSelectionKeyword,
  170.                                                 fileList);
  171.                         myErr := AEDisposeDesc(fileList);
  172.  
  173.                         if (myErr = noErr) then
  174.                           myErr := AESend(aeEvent, aeReply,
  175.                                           kAENoReply + kAEAlwaysInteract + 
  176.                                           kAECanSwitchLayer,
  177.                                           kAENormalPriority,
  178. kAEDefaultTimeout,
  179.                                           nil, nil);
  180.                       end;
  181.                   end;
  182.                 myErr := AEDisposeDesc(aeEvent);
  183.               end;
  184.           end;
  185.       end;
  186.     if (DirAlias <> nil) then
  187.       DisposHandle(Handle(DirAlias));
  188.     if (FileAlias <> nil) then
  189.       DisposHandle(Handle(FileAlias));
  190.     OpenSelection := myErr;
  191.   end;
  192.  
  193. end.
  194.  
  195.  
  196.