home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / mac / programm / 15387 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  7.0 KB

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