Important: The information in this document is obsolete and should not be used for new development.
Writing an Object-Counting Function
To handle object specifier records that specify tests, your application should provide an object-counting function (unless it specifieskAEIDoWhose
as just described). Your object-counting function should be able to count the number of elements of a given object class in a given container. For example, if your application supports Apple event objects that belong to the object classcText
in the Text suite, your application should provide an object-counting function that can count Apple event objects of each element class listed in the definition ofcText
in the Apple Event Registry: Standard Suites. In this case, your application should provide an object-counting function that can count the number of words, items, or characters in a text object.You specify your object-counting function with the
AESetObjectCallbacks
function. Whenever it is resolving an object specifier record and it requires a count of the number of elements in a given container, the Apple Event Manager calls your object-counting function.Here's the declaration for a sample object-counting function:
FUNCTION MyCountObjects (desiredClass: DescType; containerClass: DescType; containerToken: AEDesc; VAR result: LongInt): OSErr;The Apple Event Manager passes the following information to your object-counting function: the object class ID of the Apple event objects to count, the object class of their container, and a token identifying their container. (The container class can be useful if you want to use one token type for several object classes.) Your object-counting function uses this information to count the number of Apple event objects of the specified object class in the specified container. After counting the Apple event objects, your application should return thenoErr
result code and, in theresult
parameter, the number of Apple event objects counted.Listing 6-10 shows an application-defined function,
MyCountObjects
, that counts the number of objects for any object class supported by the application.Listing 6-10 An object-counting function
FUNCTION MyCountObjects (desiredClass: DescType; containerClass: DescType; containerToken: AEDesc; VAR result: LongInt): OSErr; VAR window: WindowPtr; BEGIN result := 0; IF desiredClass = cWindow THEN BEGIN IF containerClass = typeNull THEN BEGIN {count the number of windows} window := FrontWindow; WHILE window <> NIL DO BEGIN result := result + 1; window := WindowPtr(WindowPeek(window)^.nextWindow); END; {of while} END; MyCountObjects := noErr; END {of cWindow} ELSE IF desiredClass = cWord THEN {count the number of words in the container} MyCountObjects := MyCountWords(containerClass, containerToken, result) ELSE IF desiredClass = cParagraph THEN {count the number of paragraphs in the container} MyCountObjects := MyCountParas(containerClass, containerToken, result) ELSE {this app does not support any other object classes} MyCountObjects := kObjectClassNotFound; END;