home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / PNL Libraries / MyProcesses.p < prev    next >
Encoding:
Text File  |  1995-10-30  |  12.6 KB  |  428 lines  |  [TEXT/CWIE]

  1. unit MyProcesses;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Types, Files, Memory, Processes;
  7.  
  8.     const
  9.         application = 'APPL';
  10.  
  11.     function FindApplication (creator: OSType; var fs: FSSpec): OSErr;
  12.     function FindProcess (creator, typ: OSType; var process: ProcessSerialNumber; var fs: FSSpec): boolean;
  13.     function FindAppWithHint(vrn:integer; dirID:longint; creator, typ: OSType; var app:FSSpec; var psn: ProcessSerialNumber; var isrunning:Boolean):OSErr;
  14.     function FindControlPanel (fcreator: OSType; var fs: FSSpec): OSErr;
  15.     function FindInFolder(vrn:integer; dirID:longint; fcreator, ftype: OSType; var fs:FSSpec):OSErr;
  16.  
  17.     function IsRunning (creator, typ: OSType): boolean;
  18.  
  19.     function LaunchWithDocument (creator, typ: OSType; fs: FSSpec; tofront: boolean):OSErr;
  20.     function LaunchApp (creator, typ: OSType; tofront: boolean):OSErr;
  21.     function LaunchAppWithHint(vrn:integer; dirID:longint; creator, typ: OSType; tofront: boolean):OSErr;
  22.     function LaunchFSSpec (var fs: FSSpec; tofront: boolean):OSErr;
  23.  
  24.     function SendQuitToApplication(process: processSerialNumber):OSErr;
  25.     procedure QuitApplication (creator, typ: OSType);
  26.  
  27.     function OpenControlPanel (fcreator: OSType): boolean;
  28.     function TellFinderToLaunch (fs: FSSpec; tofront: boolean): boolean;
  29.     
  30. implementation
  31.  
  32.     uses
  33.         AppleEvents, Aliases, Folders, GestaltEqu, 
  34.         MySystemGlobals, MyFileSystemUtils, MyUtils, MyAEUtils;
  35.  
  36.     procedure AddFSSToAEList (var list: AEDescList; row: integer; var fs: FSSpec);
  37.         var
  38.             fileAlias: AliasHandle;
  39.             err: OSErr;
  40.     begin
  41.         err := NewAlias(nil, fs, fileAlias);
  42.         if err = noErr then begin
  43.             HLock(handle(fileAlias));
  44.             err := AEPutPtr(list, row, typeAlias, ptr(fileAlias^), fileAlias^^.aliasSize);
  45.             DisposeHandle(handle(fileAlias));
  46.         end;
  47.     end;
  48.  
  49.     function FindInFolder(vrn:integer; dirID:longint; fcreator, ftype: OSType; var fs:FSSpec):OSErr;
  50.         var
  51.             err:OSErr;
  52.             pb: HParamBlockRec;
  53.             i: integer;
  54.     begin
  55.         fs.vRefNum := vrn;
  56.         fs.parID := dirID;
  57.         i := 1;
  58.         repeat
  59.             pb.ioNamePtr := @fs.name;
  60.             pb.ioVRefNum := vrn;
  61.             pb.ioDirID := dirID;
  62.             pb.ioFDirIndex := i;
  63.             i := i + 1;
  64.             err := PBHGetFInfoSync(@pb);
  65.             if err = noErr then begin
  66.                 if (pb.ioFlFndrInfo.fdCreator = fcreator) & (pb.ioFlFndrInfo.fdType = ftype) then begin
  67.                     leave;
  68.                 end;
  69.             end;
  70.         until (err <> noErr);
  71.         FindInFolder := err;
  72.     end;
  73.  
  74.     function FindControlPanel (fcreator: OSType; var fs: FSSpec): OSErr;
  75.         var
  76.             err: OSErr;
  77.     begin
  78.         err := FindFolder(kOnSystemDisk, kControlPanelFolderType, false, fs.vRefNum, fs.parID);
  79.         if err = noErr then begin
  80.             err := FindInFolder(fs.vRefNum, fs.parID, fcreator, 'cdev', fs);
  81.         end;
  82.         FindControlPanel := err;
  83.     end;
  84.  
  85.     function TellFinderToLaunch (fs: FSSpec; tofront: boolean): boolean;
  86.         var
  87.             process: ProcessSerialNumber;
  88.             err, junk: OSErr;
  89.             targetAddress: AEDesc;
  90.             fileList: AEDescList;
  91.             theEvent, theReply: AppleEvent;
  92.             sendmode: AESendMode;
  93.             gv: longint;
  94.             finder_fs: FSSpec;
  95.     begin
  96.         err := -1;
  97.         if (Gestalt(gestaltFinderAttr, gv) = noErr) & TPbtst(gv, gestaltOSLCompliantFinder) then begin
  98.             if FindProcess('MACS', 'FNDR', process, finder_fs) then begin
  99.                 AECreate(theEvent);
  100.                 AECreate(theReply);
  101.                 AECreate(fileList);
  102.                 AECreate(targetAddress);
  103.                 err := AECreateDesc(typeProcessSerialNumber, @process, sizeof(process), targetAddress);
  104.                 if err = noErr then begin
  105.                     err := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  106.                 end;
  107.                 AEDestroy(targetAddress);
  108.                 if err = noErr then begin
  109.                     err := AECreateList(nil, 0, false, fileList);
  110.                 end;
  111.                 if err = noErr then begin
  112.                     AddFSSToAEList(fileList, 1, fs);
  113.                 end;
  114.                 if err = noErr then begin
  115.                     err := AEPutParamDesc(theEvent, keyDirectObject, fileList);
  116.                 end;
  117.                 if err = noErr then begin
  118.                     sendmode := kAENoReply;
  119.                     if not tofront then begin
  120.                         sendmode := sendmode + kAENeverInteract;
  121.                     end;
  122.                     err := AESend(theEvent, theReply, sendmode, kAEHighPriority, kNoTimeOut, nil, nil);
  123.                 end;
  124.                 AEDestroy(theEvent);
  125.                 AEDestroy(theReply);
  126.                 AEDestroy(fileList);
  127.                 if (err = noErr) & tofront then begin
  128.                     junk := SetFrontProcess(process);
  129.                 end;
  130.             end;
  131.         end;
  132.         TellFinderToLaunch := err = noErr;
  133.     end;
  134.  
  135.     function OpenControlPanel (fcreator: OSType): boolean;
  136.         var
  137.             fs: FSSpec;
  138.     begin
  139.         OpenControlPanel := false;
  140.         if FindControlPanel(fcreator, fs) = noErr then begin
  141.             OpenControlPanel := TellFinderToLaunch(fs, true);
  142.         end;
  143.     end;
  144.  
  145.     function ConfirmApplicationExists (creator: OSType; var fs: FSSpec): OSErr;
  146.         var
  147.             err: OSErr;
  148.             info: FInfo;
  149.     begin
  150.         err := HGetFInfo(fs.vRefNum, fs.parID, fs.name, info);
  151.         if err = noErr then begin
  152.             if (info.fdType <> application) or (info.fdCreator <> creator) then begin
  153.                 err := afpItemNotFound;
  154.             end; (* if *)
  155.         end; (* if *)
  156.         ConfirmApplicationExists := err;
  157.     end;
  158.  
  159.     function FindApplication (creator: OSType; var fs: FSSpec): OSErr;
  160.         var
  161.             i: integer;
  162.             pbdt: DTPBRec;
  163.             crdate: longint;
  164.             oe: OSErr;
  165.             found: boolean;
  166.     begin
  167.         found := false;
  168.         if system7 then begin
  169.             i := 1;
  170.             repeat
  171.                 fs.vRefNum := 0;
  172.                 oe := GetVolInfo(fs.name, fs.vRefNum, i, crdate);
  173.                 i := i + 1;
  174.                 if oe = noErr then begin
  175.                     with pbdt do begin
  176.                         fs.name := '';
  177.                         ioNamePtr := @fs.name;
  178.                         ioVRefNum := fs.vRefNum;
  179.                         oe := PBDTGetPath(@pbdt);
  180.                         if oe = noErr then begin
  181.                             ioIndex := 0;
  182.                             ioFileCreator := creator;
  183.                             oe := PBDTGetAPPLSync(@pbdt);
  184.                             if oe = noErr then begin
  185.                                 fs.parID := pbdt.ioAPPLParID;
  186.                                 found := ConfirmApplicationExists(creator,fs)=noErr;
  187.                             end;
  188.                         end;
  189.                     end;
  190.                     oe := noErr;
  191.                 end;
  192.             until found or (oe <> noErr);
  193.         end;
  194.         if found then begin
  195.             oe := noErr;
  196.         end
  197.         else begin
  198.             oe := afpItemNotFound;
  199.             fs.vRefNum := 0;
  200.             fs.parID := 2;
  201.             fs.name := '';
  202.         end;
  203.         FindApplication := oe;
  204.     end;
  205.  
  206.     function FindProcess (creator, typ: OSType; var process: ProcessSerialNumber; var fs: FSSpec): boolean;
  207.         var
  208.             info: ProcessInfoRec;
  209.     begin
  210.         FindProcess := false;
  211.         if has_LaunchControl then begin
  212.             process.highLongOfPSN := 0;
  213.             process.lowLongOfPSN := kNoProcess;
  214.             info.processInfoLength := sizeof(ProcessInfoRec);
  215.             info.processName := nil;
  216.             info.processAppSpec := @fs;
  217.             while GetNextProcess(process) = noErr do begin
  218.                 if GetProcessInformation(process, info) = noErr then begin
  219.                     if (info.processType = longint(typ)) and (info.processSignature = creator) then begin
  220.                         FindProcess := true;
  221.                         leave;
  222.                     end;
  223.                 end;
  224.             end;
  225.         end;
  226.     end;
  227.  
  228.     function IsRunning (creator, typ: OSType): boolean;
  229.         var
  230.             process: ProcessSerialNumber;
  231.             fs: FSSpec;
  232.     begin
  233.         IsRunning := FindProcess(creator, typ, process, fs);
  234.     end;
  235.     
  236.     procedure PrepareToLaunch (var theEvent: AppleEvent; tofront: boolean; var launchThis: LaunchParamBlockRec);
  237.         var
  238.             oe: OSErr;
  239.             launchDesc: AEDesc;
  240.     begin
  241.         oe := AECoerceDesc(theEvent, typeAppParameters, launchDesc);
  242.         HLock(handle(launchDesc.dataHandle));
  243.         launchThis.launchAppParameters := AppParametersPtr(launchDesc.dataHandle^);
  244.         launchThis.launchBlockID := extendedBlock;
  245.         launchThis.launchEPBLength := extendedBlockLen;
  246.         launchThis.launchFileFlags := 0;
  247.         launchThis.launchControlFlags := launchContinue + launchNoFileFlags;
  248.         if not tofront then begin
  249.             launchThis.launchControlFlags := launchThis.launchControlFlags + launchDontSwitch;
  250.         end;
  251.     end;
  252.  
  253.     function LaunchApplicationOptionallyMinimum(var launchThis: LaunchParamBlockRec):OSErr;
  254.         var
  255.             err:OSErr;
  256.     begin
  257.         err := LaunchApplication(@launchThis);
  258.         if err = memFullErr then begin
  259.             launchThis.launchControlFlags := BOR(launchThis.launchControlFlags, launchUseMinimum);
  260.             err := LaunchApplication(@launchThis);
  261.         end;
  262.         LaunchApplicationOptionallyMinimum:=err;
  263.     end;
  264.     
  265.     function LaunchWithDocument (creator, typ: OSType; fs: FSSpec; tofront: boolean):OSErr;
  266.         var
  267.             psn: ProcessSerialNumber;
  268.             targetAddress: AEDesc;
  269.             theEvent, theReply: AppleEvent;
  270.             fileList: AEDescList;
  271.             app_fs: FSSpec;
  272.             launchThis: LaunchParamBlockRec;
  273.             oe: OSErr;
  274.             sendmode: AESendMode;
  275.             t, c: longint;
  276.     begin
  277.         LaunchWithDocument := -1;
  278.         PurgeSpace(t, c);
  279.         if has_LaunchControl & (c > 4096) then begin
  280.             if FindProcess(creator, typ, psn, app_fs) then begin
  281.                 oe := AECreateDesc(typeProcessSerialNumber, @psn, sizeof(psn), targetAddress);
  282.                 oe := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  283.                 oe := AEDisposeDesc(targetAddress);
  284.                 
  285.                 oe := AECreateList(nil, 0, false, fileList);
  286.                 AddFSSToAEList(fileList, 1, fs);
  287.                 oe := AEPutParamDesc(theEvent, keyDirectObject, fileList);
  288.                 oe := AEDisposeDesc(fileList);
  289.                 
  290.                 sendmode := kAENoReply;
  291.                 if not tofront then begin
  292.                     sendmode := sendmode + kAENeverInteract;
  293.                 end;
  294.                 oe := AESend(theEvent, theReply, sendmode, kAEHighPriority, kNoTimeOut, nil, nil);
  295.                 oe := AEDisposeDesc(theEvent);
  296.                 oe := AEDisposeDesc(theReply);
  297.                 if tofront then begin
  298.                     LaunchWithDocument := SetFrontProcess(psn);
  299.                 end;
  300.             end
  301.             else begin
  302.                 if FindApplication(creator, app_fs) = noErr then begin
  303.                     oe := AECreateDesc(typeApplSignature, @creator, sizeof(creator), targetAddress);
  304.                     oe := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  305.                     oe := AEDisposeDesc(targetAddress);
  306.                     
  307.                     oe := AECreateList(nil, 0, false, fileList);
  308.                     AddFSSToAEList(fileList, 1, fs);
  309.                     oe := AEPutParamDesc(theEvent, keyDirectObject, fileList);
  310.                     oe := AEDisposeDesc(fileList);
  311.                     
  312.                     launchThis.launchAppSpec := @app_fs;
  313.                     PrepareToLaunch(theEvent, tofront, launchThis);
  314.                     LaunchWithDocument := LaunchApplicationOptionallyMinimum(launchThis);
  315.                     oe := AEDisposeDesc(theEvent);
  316.                 end;
  317.             end;
  318.         end;
  319.     end;
  320.  
  321.     function LaunchFSSpec (var fs: FSSpec; tofront: boolean):OSErr;
  322.         var
  323.             launchThis: LaunchParamBlockRec;
  324.     begin
  325.         LaunchFSSpec := -1;
  326.         if has_LaunchControl then begin
  327.             launchThis.launchBlockID := extendedBlock;
  328.             launchThis.launchEPBLength := extendedBlockLen;
  329.             launchThis.launchFileFlags := 0;
  330.             launchThis.launchControlFlags := launchContinue + launchNoFileFlags + launchUseMinimum;
  331.             if not tofront then begin
  332.                 launchThis.launchControlFlags := launchThis.launchControlFlags + launchDontSwitch;
  333.             end;
  334.             launchThis.launchAppSpec := @fs;
  335.             launchThis.launchAppParameters := nil;
  336.             LaunchFSSpec := LaunchApplicationOptionallyMinimum(launchThis);
  337.         end;
  338.     end;
  339.  
  340.     function LaunchApp (creator, typ: OSType; tofront: boolean):OSErr;
  341.         var
  342.             psn: ProcessSerialNumber;
  343.             app: FSSpec;
  344.     begin
  345.         LaunchApp := -1;
  346.         if has_LaunchControl then begin
  347.             if FindProcess(creator, typ, psn, app) then begin
  348.                 if tofront then begin
  349.                     LaunchApp := SetFrontProcess(psn);
  350.                 end else begin
  351.                     LaunchApp := noErr;
  352.                 end;
  353.             end
  354.             else begin
  355.                 if FindApplication(creator, app) = noErr then begin
  356.                     LaunchApp := LaunchFSSpec(app, tofront);
  357.                 end;
  358.             end;
  359.         end;
  360.     end;
  361.  
  362.     function FindAppWithHint(vrn:integer; dirID:longint; creator, typ: OSType; var app:FSSpec; var psn: ProcessSerialNumber; var isrunning:Boolean):OSErr;
  363.         var
  364.             err:OSErr;
  365.     begin
  366.         err := -1;
  367.         if has_LaunchControl then begin
  368.             if FindProcess(creator, typ, psn, app) then begin
  369.                 isrunning := true;
  370.                 err := noErr;
  371.             end else begin
  372.                 isrunning := false;
  373.                 err := FindInFolder(vrn, dirID, creator, typ, app);
  374.                 if err <> noErr then begin
  375.                     err := FindApplication(creator, app);
  376.                 end;
  377.             end;
  378.         end;
  379.         FindAppWithHint := err;
  380.     end;
  381.     
  382.     function LaunchAppWithHint(vrn:integer; dirID:longint; creator, typ: OSType; tofront: boolean):OSErr;
  383.         var
  384.             err:OSErr;
  385.             psn: ProcessSerialNumber;
  386.             app: FSSpec;
  387.             isrunning: Boolean;
  388.     begin
  389.         err := FindAppWithHint(vrn, dirID, creator, typ,  app, psn, isrunning);
  390.         if err = noErr then begin
  391.             if isrunning then begin
  392.                 if tofront then begin
  393.                     err := SetFrontProcess(psn);
  394.                 end;
  395.             end else begin
  396.                 err := LaunchFSSpec(app, tofront);
  397.             end;
  398.         end;
  399.         LaunchAppWithHint := err;
  400.     end;
  401.     
  402.     function SendQuitToApplication(process: processSerialNumber):OSErr;
  403.         var
  404.             err, junk: OSErr;
  405.             targetAddress: AEAddressDesc;
  406.             AEvent, AReply: AppleEvent;
  407.     begin
  408.         junk := AECreateDesc(typeProcessSerialNumber, @process, SizeOf(process), targetAddress);
  409.         junk := AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, AEvent);
  410.         junk := AEDisposeDesc(targetAddress);
  411.         err := AESend(AEvent, AReply, kAENoReply, kAEHighPriority, 5 * 60, nil, nil);
  412.         junk := AEDisposeDesc(AEvent);
  413.         junk := AEDisposeDesc(AReply);
  414.         SendQuitToApplication := err;
  415.     end;
  416.     
  417.     procedure QuitApplication (creator, typ: OSType);
  418.         var
  419.             junk:OSErr;
  420.             process: processSerialNumber;
  421.             fs: FSSpec;
  422.     begin
  423.         if FindProcess(creator, typ, process, fs) then begin
  424.             junk:=SendQuitToApplication(process);
  425.         end;
  426.     end;
  427.  
  428. end.