home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-04-20 | 4.1 KB | 129 lines | [TEXT/PJMM] |
- unit MyAppleEvents;
-
- { This program was written by Peter N Lewis, Mar 1992 in THINK Pascal 4.0.1 }
-
- interface
-
- function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
- { function DoOApp: OSErr }
- { function DoODoc (fs: FSSpec): OSErr }
- { function DoPrint (fs: FSSpec): OSErr }
- { function DoQuit: OSErr}
-
- implementation
-
- uses
- AppleTalk, PPCToolbox, Processes, EPPC, Notification, AppleEvents, MyUtilities;
-
- function DoOApp (p: ptr): OSErr;
- inline
- $205F, $4E90;
-
- function DoDocs (fs: FSSpec; p: ptr): OSErr;
- inline
- $205F, $4E90;
-
- function DoQuit (p: ptr): OSErr;
- inline
- $205F, $4E90;
-
- const
- kPatienceLevel = 1000; { <aevt> ticks we wait for response }
- kSysEnvironsVersion = 1;
- kOSEvent = app4Evt; {event used by MultiFinder}
- kSuspendResumeMessage = 1; {high byte of suspend/resume event message}
- kResumeMask = 1; {bit of message field for resume vs. suspend}
- kMouseMovedMessage = $FA; {high byte of mouse-moved event message}
- kNoEvents = 0; {no events mask}
- kNoListChosen = -1;
-
- function GotRequiredParams (theAppleEvent: AppleEvent): OSErr; { <aevt> }
- var
- typeCode: DescType;
- actualSize: Size;
- err: OSErr;
- begin
- err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, typeCode, nil, 0, actualSize); { nil ok: need only function result }
- if err = errAEDescNotFound then { we got all the required params: all is ok }
- GotRequiredParams := noErr
- else if err = noErr then
- GotRequiredParams := errAEEventNotHandled
- else
- GotRequiredParams := err;
- end; { GotRequiredParams }
-
- function HandleOAPP (theAppleEvent, reply: AppleEvent; openappp: ptr): OSErr;{ <aevt> }
- var
- oe: OSErr;
- begin
- { We don't expect any params at all, but check in case the client requires any }
- oe := GotRequiredParams(theAppleEvent);
- oe := DoOApp(openappp);
- HandleOAPP := oe;
- end;
-
- function HandleDocs (theAppleEvent, reply: AppleEvent; dodocp: ptr): OSErr; { <aevt> }
- var
- myFSS: FSSpec;
- docList: AEDescList;
- index, itemsInList: LONGINT;
- actualSize: Size;
- keywd: AEKeyword;
- typeCode: descType;
- ignoreWPtr: WindowPtr;
- oe, ooe: OSErr;
- begin
- oe := AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, docList);
- if oe = noErr then begin
- ooe := GotRequiredParams(theAppleEvent);
- { now get each alias from the list (as an FSSSpec) and open the associated file. }
- oe := AECountItems(docList, itemsInList);
- for index := 1 to itemsInList do begin
- ooe := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
- { coercion does alias->fsspec }
- if ooe = noErr then
- ooe := DoDocs(myFSS, dodocp);
- end;
- ooe := AEDisposeDesc(docList);
- end;
- HandleDocs := oe;
- end; { HandleDocs }
-
- function HandleQUIT (theAppleEvent, reply: AppleEvent; quitp: ptr): OSErr; { <aevt> }
- var
- oe: OSErr;
- errStr: Str255;
- willQuit: Boolean; { did the user allow the quit or cancel }
- begin
- { We don't expect any params at all, but check in case the client requires any }
- oe := GotRequiredParams(theAppleEvent);
- oe := DoQuit(quitp); { set global boolean: app will exit at end of event loop }
- if reply.dataHandle <> nil then { a reply is sought }
- begin
- if oe = noErr then
- errStr := 'OK'
- else
- errStr := 'user cancelled quit';
- oe := AEPutParamPtr(reply, 'errs', 'TEXT', Ptr(@errStr[1]), length(errStr));
- end;
- HandleQUIT := oe;
- end;
-
- {$S Init}
- function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
- var
- aevtErr: OSErr;
- begin
- aevtErr := noErr;
- if (aevtErr = noErr) and (DoOApp <> nil) then
- aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, longInt(DoOApp), false);
- if (aevtErr = noErr) and (DoODoc <> nil) then
- aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleDocs, longInt(DoODoc), false);
- if (aevtErr = noErr) and (DoPrint <> nil) then
- aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandleDocs, longInt(DoPrint), false);
- if (aevtErr = noErr) and (DoQuit <> nil) then
- aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, longInt(DoQuit), false);
- InitAppleEvents := aevtErr;
- end;
-
- end.