Unit AEUtility; { *************************************************************************************** Utility Routines used by the AE Handlers. *************************************************************************************** } interface uses Memory, QuickDraw, Packages, Menus, Events, Fonts, Scrap, ToolUtils,Resources, Errors, Palettes, LowMem, AppleEvents, AEObjects, AERegistry, Errors, Globals, Utilities, AEInterface; type AppleEventPtr = ^AppleEvent; ObjectToken = record containerWindow: WindowPtr; containerInfo: InfoPtr; containerType: DescType; name: DescType; index: LongInt; thru: LongInt; end; TokenPtr = ^ObjectToken; ExtendedPtr = ^Extended; BooleanPtr = ^Boolean; PointPtr = ^Point; ShortArray = array[0..MaxLine] of integer; NewToken = record containerWindow: WindowPtr; containerInfo: InfoPtr; containerType: DescType; name: DescType; index: LongInt; thru: LongInt; fname:str255; end; NewObjectPtr = ^NewToken; procedure MakeTokenDesc(theWindow:WindowPtr; theType:DescType; var outToken:AEDesc); procedure MakeDefaultToken(var outToken:AEDesc); procedure MakeWindowToken(inWindow:WindowPtr; var outToken:AEDesc); procedure MakeNewTokenDesc(theName:str255; theType:DescType; var outToken:AEDesc); function CountObjects(inType: DescType):integer; procedure AbsoluteOrdinal(keyData: AEDesc; theToken:TokenPtr; max:integer); function GetRange(keyData: AEDesc; var outStart: LongInt; var outEnd: LongInt): OSErr; function GetTargetFromSignature (processSig: OSType; var targetDesc: AEAddressDesc): OSErr; function GetAEBoolean(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outValue:Boolean):OSErr; function GetAELong(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outInteger:LongInt):OSErr; function GetAEShort(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outInteger:integer):OSErr; function GetAEExtended(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outValue:extended):OSErr; function GetAEStr255(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outString:str255):OSerr; function GetAEEnum(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outEnum:DescType):OSErr; function GetAEShortFromEnum(var inAppleEvent:AppleEvent; inKey: AEKeyWord; inList: integer; var outInteger:integer):OSErr; function GetAEEnumList(var theAppleEvent:AppleEvent; inKey: AEKeyWord; var theList:AEDescList):OSerr; function CheckEnumList(theList:AEDescList; theCode:DescType):Boolean; function GetAEPoint(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outPt:Point):OSErr; function GetAERect(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outRect:Rect):OSErr; function GetAERGB(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outColor:RGBColor):OSErr; function GetAEName(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outString:str255):OSerr; function AEIntegerFromKeyDesc(theAERecord: AERecord; theAEKeyword: AEKeyword; var outInteger:LongInt):OSErr; function AEStringFromKeyDesc(theAERecord: AERecord; theAEKeyword: AEKeyword; var outString: str255): OSErr; function LongFromDesc(theAERecord: AERecord; var outValue: LongInt): OSErr; function StringFromDesc(theAERecord: AERecord; var outString: str255): OSErr; function ExtendedFromDesc(theAERecord: AERecord; var outValue: Extended): OSErr; function EnumFromDesc(theAERecord: AERecord; var outValue: DescType): OSErr; function ShortFromDesc(theAERecord: AERecord; var outValue: integer): OSErr; function GetShortProperty(var inAppleEvent:AppleEvent; min,max:integer): integer; function ByteFromDesc(theAERecord: AERecord; var outValue: integer): OSErr; function PointFromDesc(theAERecord: AERecord; var outPt: Point): OSErr; function EnumToLong(inValue: DescType; inList : integer):LongInt; function LongToEnum(inValue:LongInt; inList : integer):DescType; function GetLongProperty(var inAppleEvent:AppleEvent): LongInt; function GetBooleanProperty(var inAppleEvent:AppleEvent): Boolean; function GetExtendedProperty(var inAppleEvent:AppleEvent): Extended; function GetEnumProperty(var inAppleEvent:AppleEvent; inList:integer): integer; function GetRectProperty(var inAppleEvent:AppleEvent): Rect; function GetPointProperty(var inAppleEvent:AppleEvent): Point; Function ReturnBooleanProperty(var outReply:AppleEvent; theValue:Boolean): OSErr; Function ReturnShortProperty(var outReply:AppleEvent; theValue:integer): OSErr; Function ReturnEnumProperty(var outReply:AppleEvent; inList:integer; inValue:integer): OSErr; Function ReturnLongProperty(var outReply:AppleEvent; theValue:LongInt): OSErr; Function ReturnExtendedProperty(var outReply:AppleEvent; theValue:extended): OSErr; function ReturnPointProperty(var outReply:AppleEvent; inPt:Point): OSErr; function ReturnRectProperty(var outReply:AppleEvent; inRect:Rect): OSErr; function AEAddShortArray(maxIndex:LongInt; var outArray:ShortArray ):AEDescList; procedure SaveCurrent(inToken: TokenPtr); procedure RestoreCurrent; var sCurInfo:InfoPtr; ActivePic:Boolean; gObjIndex:LongInt; implementation procedure SaveCurrent(inToken: TokenPtr); var theWindow:WindowPtr; begin sCurInfo := nil; theWindow := inToken^.containerWindow; if theWindow = nil then exit(SaveCurrent); if AEisActive = false then begin AEisActive := true; CurrentKind := WindowPeek(theWindow)^.WindowKind; CurrentWPtr := theWindow; SetPort(theWindow); sCurInfo := info; if CurrentKind = PicKind then begin info := inToken^.containerInfo; ActivePic := sCurInfo^.PidNum = info^.PidNum; end else ActivePic := false; end; end; procedure RestoreCurrent; var kind:integer; begin if AEisActive then begin AEisActive := false; info := sCurInfo; kind := CurrentWindow; ActivePic := false; end; end; procedure MakeTokenDesc(theWindow:WindowPtr; theType:DescType; var outToken:AEDesc); var kind:integer; theError:OSErr; theToken:ObjectToken; begin kind := WindowPeek(theWindow)^.WindowKind; if kind = PicKind then theToken.containerInfo := pointer(WindowPeek(theWindow)^.RefCon) else theToken.containerInfo := info; theToken.containerWindow := theWindow; theToken.containerType := theType; theToken.name := typeNull; theToken.index := 0; theToken.thru := 0; theError := AECreateDesc(theType, @theToken, SizeOf(ObjectToken), outToken); end; procedure MakeNewTokenDesc(theName:str255; theType:DescType; var outToken:AEDesc); var theError:OSErr; theToken:NewToken; begin theToken.containerWindow := nil; theToken.containerInfo := info; theToken.containerType := theType; theToken.name := typeNull; theToken.index := 0; theToken.thru := 0; theToken.fname := theName; theError := AECreateDesc(theType, @theToken, SizeOf(NewToken), outToken); end; procedure MakeWindowToken(inWindow:WindowPtr; var outToken:AEDesc); var theError:OSErr; theToken:ObjectToken; kind:integer; begin kind := WindowPeek(inWindow)^.WindowKind; case kind of PicKind: MakeTokenDesc(inWindow, 'Ipic', outToken); HistoKind: MakeTokenDesc(inWindow, 'wHST', outToken); LUTKind: MakeTokenDesc(inWindow, 'wLUT', outToken); ToolKind: MakeTokenDesc(inWindow, 'wMAP', outToken); InfoKind: MakeTokenDesc(inWindow, 'wINF', outToken); ResultsKind: MakeTokenDesc(inWindow, 'wRes', outToken); TextKind: MakeTokenDesc(inWindow, 'wTXT', outToken); CalibrationPlotKind: MakeTokenDesc(inWindow, 'wCAL', outToken); PasteControlKind: MakeTokenDesc(inWindow, 'wPCW', outToken); end; end; procedure MakeDefaultToken(var outToken:AEDesc); var kind:integer; begin kind := CurrentWindow; if CurrentWPtr = nil then MakeTokenDesc(nil,'cwin',outToken) else if CurrentKind = PicKind then MakeWindowToken(CurrentWPtr, outToken) else if CurrentKind = HistoKind then MakeWindowToken(CurrentWPtr, outToken) else if CurrentKind = ResultsKind then MakeWindowToken(CurrentWPtr, outToken) else if CurrentKind = TextKind then MakeWindowToken(CurrentWPtr, outToken) else if nPics >= 1 then MakeWindowToken(info^.wptr,outToken) else MakeTokenDesc(CurrentWPtr,'cwin',outToken); end; function CountObjects(inType: DescType):integer; var n, iLoop:LongInt; theWindow:WindowPtr; found:Boolean; theInfo:InfoPtr; begin n := 0; if inType = 'cwin' then begin { number of open windows } theWindow := FrontWindow; found := false; WHILE theWindow <> NIL DO BEGIN n := n + 1; theWindow := WindowPtr(WindowPeek(theWindow)^.nextWindow); END; {of while} end else if inType = 'Ipic' then n := nPics else if inType = 'Stak' then for iLoop := 1 to nPics do begin theInfo := pointer(WindowPeek(PicWindow[iLoop])^.RefCon); if (theInfo^.StackInfo <> nil) then n := n + 1; end else if inType = 'Slic' then begin if info^.StackInfo <> nil then n := info^.StackInfo^.nSlices else n:= 0; end else if (inType = 'ResW') or (inType = 'Hist') or (inType = 'PlDa') or (inType = 'PCW ') then n := 1 else if (inType = 'xCor') or (inType = 'yCor') then n := nCoordinates else if (inType = 'Red ') or (inType = 'Gree') or (inType = 'Blue') then n := 256 else n := mCount; { assume a measurement } CountObjects := n; end; { *************************************************************************************** Make a property token. It doesn't care about the container type. *************************************************************************************** } function GetRange(keyData: AEDesc; var outStart: LongInt; var outEnd: LongInt): OSErr; VAR lp: integerPtr; theError : OSErr; typeCode:DescType; actualSize:LongInt; theResult:AERecord; theAEObject : AEDesc; theList:AEDescList; theRecord : AERecord; theAEToken : AEDesc; i, theCount: LongInt; theAEKeyword:AEKeyword; theToken:ObjectToken; tp:TokenPtr; buffer: LongInt; begin theError := AECoerceDesc(keyData, typeAERecord, theRecord); theError := AEGetParamDesc( theRecord, keyAERangeStart, 'obj ', theAEObject ); theError := AEResolve (theAEObject, kAEIDoMinimum, @theAEToken); { THIS WORKS! tp := TokenPtr(theAEToken.dataHandle^); outStart := tp^.index; } outStart := gObjIndex; theError := AEDisposeDesc(theAEObject); theError := AEDisposeDesc(theAEToken); theError := AEGetParamDesc( theRecord, keyAERangeStop, 'obj ', theAEObject ); {typeType} theError := AEResolve (theAEObject, kAEIDoMinimum, @theToken); { BUT THIS FAILS! tp := TokenPtr(theAEToken.dataHandle^); outEnd := tp^.index; } outEnd := gObjIndex; theError := AEDisposeDesc(theAEObject); theError := AEDisposeDesc(theAEToken); theError := AEDisposeDesc(theRecord); GetRange := theError; end; procedure AbsoluteOrdinal(keyData: AEDesc; theToken:TokenPtr; max:integer); var theEnum:DescType; begin theEnum := DescType( LongIntPtr( keyData.dataHandle^ )^ ); if theEnum = 'any ' then theToken^.index := 1 else if theEnum = 'midd' then theToken^.index := (max + 1) div 2 else if theEnum = 'all ' then begin theToken^.index := 1; theToken^.thru := max; end; end; function GetTargetFromSignature (processSig: OSType; var targetDesc: AEAddressDesc): OSErr; begin GetTargetFromSignature := AECreateDesc(typeApplSignature, @processSig, sizeof(processSig), targetDesc); end; function GetAEBoolean(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outValue:Boolean):OSErr; VAR theReturnedType:DescType; theActualSize:Size; begin GetAEBoolean:=AEGetParamPtr(inAppleEvent, inKey, typeBoolean, theReturnedType, Ptr(@outValue), Sizeof(Boolean), theActualSize); end; function GetAELong(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outInteger:LongInt):OSErr; VAR theReturnedType:DescType; theActualSize:Size; begin GetAELong:=AEGetParamPtr(inAppleEvent, inKey, typeLongInteger, theReturnedType, Ptr(@outInteger), Sizeof(LongInt), theActualSize); end; function GetAEShort(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outInteger:integer):OSErr; VAR theReturnedType:DescType; theActualSize:Size; begin GetAEShort:=AEGetParamPtr(inAppleEvent, inKey, typeShortInteger, theReturnedType, Ptr(@outInteger), Sizeof(integer), theActualSize); end; function GetAEEnum(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outEnum:DescType):OSErr; VAR theReturnedType:DescType; theActualSize:Size; begin GetAEEnum := AEGetParamPtr(inAppleEvent, inKey, typeEnumerated, theReturnedType, Ptr(@outEnum), Sizeof(DescType), theActualSize); end; function GetAERGB(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outColor:RGBColor):OSErr; VAR theReturnedType:DescType; theActualSize:Size; theError:OSErr; ptList:AEDescList; keywd:AEKeyword; index, ItemsInList:LongInt; pt:Point; val:integer; begin theError := AEGetParamPtr(inAppleEvent, inKey, typeRGBColor, theReturnedType, Ptr(@outColor), Sizeof(RGBColor), theActualSize); if theError <> noErr then begin theError := AEGetParamDesc(inAppleEvent, inKey, typeAEList, ptList); if theError = noErr then begin theError := AECountItems(ptList, itemsInList); case itemsInList of 1: begin theError := AEGetNthPtr(ptList, 1, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outColor.red := val; outColor.green := val; outColor.blue := val; end; 3: begin theError := AEGetNthPtr(ptList, 1, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outColor.red := val; theError := AEGetNthPtr(ptList, 2, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outColor.green := val; theError := AEGetNthPtr(ptList, 3, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outColor.blue := val; end; end; end; end; GetAERGB := theError; end; function GetAEShortFromEnum(var inAppleEvent:AppleEvent; inKey: AEKeyWord; inList: integer; var outInteger:integer):OSErr; VAR theReturnedType:DescType; theActualSize:Size; theEnum:DescType; begin GetAEShortFromEnum := AEGetParamPtr(inAppleEvent, inKey, typeEnumerated, theReturnedType, Ptr(@theEnum), Sizeof(DescType), theActualSize); outInteger := EnumToLong(theEnum,inList); end; function GetAEPoint(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outPt:Point):OSErr; VAR theReturnedType:DescType; theActualSize:Size; begin GetAEPoint := AEGetParamPtr(inAppleEvent, inKey, typeQDPoint, theReturnedType, Ptr(@outPt), Sizeof(Point), theActualSize); end; function GetAERect(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outRect:Rect):OSErr; VAR theError:OSErr; theReturnedType:DescType; theActualSize:Size; ptList:AEDescList; keywd:AEKeyword; index, ItemsInList:LongInt; pt:Point; val:integer; begin theError := AEGetParamPtr(inAppleEvent, inKey, typeRectangle, theReturnedType, Ptr(@outRect), Sizeof(Rect), theActualSize); if theError <> noErr then begin theError := AEGetParamDesc(inAppleEvent, inKey, typeAEList, ptList); if theError = noErr then begin theError := AECountItems(ptList, itemsInList); case itemsInList of 2: begin theError := AEGetNthPtr(ptList, 1, typeQDPoint, keywd, theReturnedType, @pt, SizeOf(Point), theActualSize); outRect.left := pt.h; outRect.top := pt.v; theError := AEGetNthPtr(ptList, 2, typeQDPoint, keywd, theReturnedType, @pt, SizeOf(Point), theActualSize); outRect.right := pt.h; outRect.bottom := pt.v; end; 3: begin theError := AEGetNthPtr(ptList, 1, typeQDPoint, keywd, theReturnedType, @pt, SizeOf(Point), theActualSize); outRect.left := pt.h; outRect.top := pt.v; theError := AEGetNthPtr(ptList, 2, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outRect.right := outRect.left + val; theError := AEGetNthPtr(ptList, 3, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outRect.bottom := outRect.top + val; end 4: begin theError := AEGetNthPtr(ptList, 1, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outRect.left := val; theError := AEGetNthPtr(ptList, 2, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outRect.top := val; theError := AEGetNthPtr(ptList, 3, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outRect.right := val; theError := AEGetNthPtr(ptList, 4, typeShortInteger, keywd, theReturnedType, @val, SizeOf(integer), theActualSize); outRect.bottom := val; end; end; end; end; GetAERect := theError; end; function GetAEExtended(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outValue:extended):OSErr; VAR theReturnedType:DescType; theActualSize:Size; begin GetAEExtended:=AEGetParamPtr(inAppleEvent, inKey, typeExtended, theReturnedType, Ptr(@outValue), Sizeof(extended), theActualSize); end; function GetAEStr255(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outString:str255):OSerr; VAR theError:OSErr; theObject:AEDesc; theLength:longInt; theName : str255; begin theError:=AEGetParamDesc(inAppleEvent, inKey, typeChar, theObject); if theError=noErr then begin with theObject do begin if (dataHandle<>nil) then begin theLength := GetHandleSize(dataHandle); if theLength > 255 then theLength := 255; BlockMoveData(dataHandle^, @outString[1], theLength); outString[0] := chr(theLength); end end end; GetAEStr255 := theError; theError := AEDisposeDesc(theObject); end; function GetAEEnumList(var theAppleEvent:AppleEvent; inKey: AEKeyWord; var theList:AEDescList):OSerr; begin GetAEEnumList := AEGetParamDesc(theAppleEvent, inKey, typeAEList, theList); end; function CheckEnumList(theList:AEDescList; theCode:DescType):Boolean; var actualSize:Size; returnedType:DescType; theEnum:DescType; keywd:AEKeyword; err:OSErr; index,itemsInList:LongInt; begin err:=AECountItems(theList, itemsInList); if err=noErr then for index:=1 to ItemsInList do begin err := AEGetNthPtr(theList, index, typeEnumerated,keywd, returnedType, @theEnum, SizeOf(DescType), actualSize); if err = noErr then begin CheckEnumList := true; exit(CheckEnumList); end; end; CheckEnumList := false; end; { function ExtractAEShortArray(theList:AEDescList; maxIndex:LongInt; var outArray:ShortArray; var outLength:LongInt ):OSerr; var theError:OSErr; returnedType:DescType; actualSize:Size; index:LongInt; theValue:integer; begin theError:= AECountItems(theList, outLength); if theError=noErr then begin if outLength > maxIndex then outLength := maxIndex; for index := 1 to outLength do begin theError := AEGetNthPtr(theList, index, typeShortInteger,'****', returnedType, @theValue, SizeOf(integer), actualSize); if theError <> noErr then theValue := 0; outArray[index] := theValue; end; end; ExtractAEShortArray := theError; end; } function AEAddShortArray(maxIndex:LongInt; var outArray:ShortArray ):AEDescList; var theError:OSErr; returnedType:DescType; actualSize:Size; index:LongInt; theValue:integer; theList:AEDescList; begin theError := AECreateList(nil, 0,false,theList); if theError=noErr then begin for index := 1 to maxIndex do begin theError := AEPutParamPtr( theList, 'data', typeShortInteger, @outArray[index], SizeOf(integer) ); end; end; AEAddShortArray := theList; end; function AEStringFromKeyDesc(theAERecord: AERecord; theAEKeyword: AEKeyword; var outString: str255): OSErr; VAR theError : OSErr; theObject : AEDesc; theLength:longInt; begin theError := AEGetKeyDesc(theAERecord, theAEKeyword, 'TEXT', theObject); if theError=noErr then begin with theObject do begin if (dataHandle<>nil) then begin theLength := GetHandleSize(dataHandle); if theLength > 255 then theLength := 255; BlockMoveData(dataHandle^, @outString[1], theLength); outString[0] := chr(theLength); end end end; AEStringFromKeyDesc := theError; theError := AEDisposeDesc(theObject); end; { *************************************************************************************** Utility Routines to get and put properties. *************************************************************************************** } function AEIntegerFromKeyDesc(theAERecord: AERecord; theAEKeyword: AEKeyword; var outInteger:LongInt):OSErr; VAR theReturnedType:DescType; theActualSize:Size; begin AEIntegerFromKeyDesc:=AEGetKeyPtr(theAERecord, theAEKeyword, typeLongInteger, theReturnedType, Ptr(@outInteger), Sizeof(LongInt), theActualSize); end; function LongFromDesc(theAERecord: AERecord; var outValue: LongInt): OSErr; VAR lp: LongIntPtr; theError : OSErr; theResult: AEDesc; begin theError := AECoerceDesc(theAERecord, typeShortInteger, theResult); if theError = noErr then begin lp := LongIntPtr(theResult.dataHandle^); outValue := lp^; theError := AEDisposeDesc(theResult); end; LongFromDesc := theError; end; function EnumFromDesc(theAERecord: AERecord; var outValue: DescType): OSErr; VAR lp: LongIntPtr; theError : OSErr; theResult: AEDesc; begin theError := AECoerceDesc(theAERecord, typeEnumerated, theResult); if theError = noErr then begin lp := LongIntPtr(theResult.dataHandle^); outValue := DescType(lp^); theError := AEDisposeDesc(theResult); end; EnumFromDesc := theError; end; function GetAEName(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outString:str255):OSerr; VAR theError:OSErr; theObject:AEDesc; theLength:longInt; theName,theArgString : str255; theList:AEDescList; theReturnedType:DescType; theActualSize:Size; ptList:AEDescList; keywd:AEKeyword; index,i,ItemsInList:LongInt; pt:Point; val:integer; begin theError := AEGetParamDesc(inAppleEvent, inKey, typeAEList, theList); theName := ''; outString := ''; if theError = noErr then begin theError := AECountItems(ptList, itemsInList); for i := 1 to itemsInList do begin theError:=AEGetParamDesc(theList, inKey, typeChar, theObject); if theError = noErr then { if (dataHandle<>nil) then begin } with theObject do begin if (descriptorType = 'TEXT') then begin theLength := GetHandleSize(dataHandle); if theLength > 255 then theLength := 255; BlockMoveData(dataHandle^, @theArgString[1], theLength); theArgString[0] := chr(theLength); end else if (descriptorType = 'shor') then begin index := IntegerPtr(dataHandle^)^; RealToString(index,3,0,theArgString); end else if (descriptorType = 'long') then begin index := LongIntPtr(dataHandle^)^; RealToString(index,4,0,theArgString); end end; outString := concat(outString, theArgString); theError := AEDisposeDesc(theObject); end; end; GetAEName := theError; theError := AEDisposeDesc(theList); end; function StringFromDesc(theAERecord: AERecord; var outString: str255): OSErr; VAR theLength: longInt; theError : OSErr; theResult: AEDesc; theList:AEDescList; index,ItemsInList,n:LongInt; theObject:AEDesc; keywd:AEKeyword; theArgString:str255; begin theError := AECoerceDesc(theAERecord, 'TEXT', theResult); if theError = noErr then with theResult do begin if (dataHandle<>nil) then begin theLength := GetHandleSize(dataHandle); if theLength > 255 then theLength := 255; BlockMoveData(dataHandle^, @outString[1], theLength); outString[0] := chr(theLength); end end; { else begin theError := AECoerceDesc(theAERecord, typeAEList, theList); if theError = noErr then begin theError := AECountItems(theList, itemsInList); outString := ''; for index := 1 to itemsInList do begin theError := AEGetNthDesc( theList, index, '****', keywd, theObject ); with theObject do begin if (descriptorType = 'TEXT') then begin theLength := GetHandleSize(dataHandle); if theLength > 255 then theLength := 255; BlockMoveData(dataHandle^, @theArgString[1], theLength); theArgString[0] := chr(theLength); end else if (descriptorType = 'shor') then begin n := IntegerPtr(dataHandle^)^; RealToString(index,3,0,theArgString); end else if (descriptorType = 'long') then begin n := LongIntPtr(dataHandle^)^; RealToString(index,4,0,theArgString); end end; outString := concat(outString, theArgString); theError := AEDisposeDesc(theObject); end; end; end; } StringFromDesc := noErr; end; function ExtendedFromDesc(theAERecord: AERecord; var outValue: Extended): OSErr; VAR lp: ExtendedPtr; theError : OSErr; theResult: AEDesc; begin theError := AECoerceDesc(theAERecord, typeExtended, theResult); if theError = noErr then begin lp := ExtendedPtr(theResult.dataHandle^); outValue := lp^; theError := AEDisposeDesc(theResult); end; ExtendedFromDesc := theError; end; function ShortFromDesc(theAERecord: AERecord; var outValue: integer): OSErr; VAR lp: integerPtr; theError : OSErr; theResult: AEDesc; begin theError := AECoerceDesc(theAERecord, typeShortInteger, theResult); if theError = noErr then begin lp := integerPtr(theResult.dataHandle^); outValue := lp^; theError := AEDisposeDesc(theResult); end; ShortFromDesc := theError; end; function ByteFromDesc(theAERecord: AERecord; var outValue: integer): OSErr; VAR lp: integerPtr; theError : OSErr; theResult: AEDesc; begin theError := ShortFromDesc(theAERecord, outValue); if outValue < 0 then outValue := 0; if outValue > 255 then outValue := 255; ByteFromDesc := theError; end; function BooleanFromDesc(theAERecord: AERecord; var outValue: Boolean): OSErr; VAR lp: BooleanPtr; theError : OSErr; theResult: AEDesc; begin theError := AECoerceDesc(theAERecord, typeBoolean, theResult); if theError = noErr then begin lp := BooleanPtr(theResult.dataHandle^); outValue := lp^; theError := AEDisposeDesc(theResult); end; BooleanFromDesc := theError; end; function PointFromDesc(theAERecord: AERecord; var outPt: Point): OSErr; VAR lp: PointPtr; theError : OSErr; theResult: AEDesc; begin theError := AECoerceDesc(theAERecord, typeQDPoint, theResult); if theError = noErr then begin lp := PointPtr(theResult.dataHandle^); outPt := lp^; theError := AEDisposeDesc(theResult); end; PointFromDesc := theError; end; procedure BlockFromDesc(theDesc: AEDesc; outPtr: Ptr; maxSize: LongInt; var actualSize:LongInt); VAR lp: PointPtr; theError : OSErr; copySize : LongInt; begin if (theDesc.dataHandle<>nil) then begin hLock(Handle(theDesc.dataHandle)); actualSize := GetHandleSize(Handle(theDesc.dataHandle)); copySize := actualSize; if copySize > maxSize then copySize := maxSize; BlockMoveData(theDesc.dataHandle^, outPtr, copySize); hUnlock(Handle(theDesc.dataHandle)); end else actualSize := 0; end; function LongToEnum(inValue:LongInt; inList : integer):DescType; type DescTypePtr = ^DescType; var rp : DescTypePtr; n : LongInt; begin rp := DescTypePtr (GetResource('Ienu', inList)^ ); n := LongInt(rp^); if (inValue <= 0) or (inValue > n) then LongToEnum := '????' else begin rp := DescTypePtr(ord(rp) + 4*inValue); LongToEnum := rp^; end end; function EnumToLong(inValue: DescType; inList : integer):LongInt; type DescTypePtr = ^DescType; var i, n:integer; theItem : str255; rp : DescTypePtr; begin rp := DescTypePtr (GetResource('Ienu', inList)^ ); n := LongInt(rp^); for i := 1 to n do begin rp := DescTypePtr(ord(rp) + 4); if rp^ = inValue then begin EnumToLong := i; exit(EnumToLong); end end; EnumToLong := 0; end; function GetBooleanProperty(var inAppleEvent:AppleEvent): Boolean; var answer: Boolean; theAEProp: AEDesc; theError : OSErr; begin theError := AEGetParamDesc(inAppleEvent, 'data', typeBoolean, theAEProp); theError := BooleanFromDesc(theAEProp, answer); GetBooleanProperty := answer; end; function GetShortProperty(var inAppleEvent:AppleEvent; min,max:integer): integer; var answer: integer; theAEProp: AEDesc; theError : OSErr; begin theError := AEGetParamDesc(inAppleEvent, 'data', typeShortInteger, theAEProp); theError := ShortFromDesc(theAEProp, answer); if (max > min) then begin if answer < min then answer := min else if answer > max then answer := max; end; GetShortProperty := answer; end; function GetEnumProperty(var inAppleEvent:AppleEvent; inList:integer): integer; var theError : OSErr; theReturnedType:DescType; theActualSize:Size; theEnum:DescType; begin theError := AEGetParamPtr(inAppleEvent, 'data', typeEnumerated, theReturnedType, Ptr(@theEnum), Sizeof(DescType), theActualSize); GetEnumProperty := EnumToLong(theEnum,inList); end; { function GetAEEnum(var inAppleEvent:AppleEvent; inKey: AEKeyWord; inList: integer; var outInteger:integer):OSErr; VAR theReturnedType:DescType; theActualSize:Size; theEnum:DescType; begin GetAEEnum := AEGetParamPtr(inAppleEvent, inKey, typeEnumerated, theReturnedType, Ptr(@theEnum), Sizeof(DescType), theActualSize); outInteger := EnumToLong(theEnum,inList); end; } function GetLongProperty(var inAppleEvent:AppleEvent): LongInt; var answer: LongInt; theAEProp: AEDesc; theError : OSErr; begin theError := AEGetParamDesc(inAppleEvent, 'data', typeLongInteger, theAEProp); theError := LongFromDesc(theAEProp, answer); GetLongProperty := answer; end; function GetExtendedProperty(var inAppleEvent:AppleEvent): Extended; var answer: Extended; theAEProp: AEDesc; theError : OSErr; begin theError := AEGetParamDesc(inAppleEvent, 'data', typeExtended, theAEProp); theError := ExtendedFromDesc(theAEProp, answer); GetExtendedProperty := answer; end; function GetPointProperty(var inAppleEvent:AppleEvent): Point; var theError : OSErr; theReturnedType:DescType; theActualSize:Size; thePoint:Point; begin theError := AEGetParamPtr(inAppleEvent, 'data', 'QDpt', theReturnedType, Ptr(@thePoint), Sizeof(Point), theActualSize); GetPointProperty := thePoint; end; function GetRectProperty(var inAppleEvent:AppleEvent): Rect; var theError : OSErr; theReturnedType:DescType; theActualSize:Size; theRect:Rect; begin theError := AEGetParamPtr(inAppleEvent, 'data', 'qdrt', theReturnedType, Ptr(@theRect), Sizeof(Rect), theActualSize); GetRectProperty := theRect; end; Function ReturnBooleanProperty(var outReply:AppleEvent; inValue:Boolean): OSErr; var theError : OSErr; begin theError := AEPutParamPtr(outReply, keyDirectObject, typeBoolean, Ptr(@inValue), sizeof(Boolean)); ReturnBooleanProperty := theError; end; Function ReturnShortProperty(var outReply:AppleEvent; inValue:integer): OSErr; var theError : OSErr; begin theError := AEPutParamPtr(outReply, keyDirectObject, typeShortInteger, Ptr(@inValue), sizeof(integer)); ReturnShortProperty := theError; end; Function ReturnEnumProperty(var outReply:AppleEvent; inList:integer; inValue:integer): OSErr; var theError : OSErr; theEnum:DescType; begin theEnum := LongToEnum(inList, inValue); theError := AEPutParamPtr(outReply, keyDirectObject, typeEnumerated, Ptr(@inValue), sizeof(DescType)); ReturnEnumProperty := theError; end; Function ReturnLongProperty(var outReply:AppleEvent; inValue:LongInt): OSErr; var theError : OSErr; begin theError := AEPutParamPtr(outReply, keyDirectObject, typeLongInteger, Ptr(@inValue), sizeof(LongInt)); ReturnLongProperty := theError; end; function ReturnExtendedProperty(var outReply:AppleEvent; inValue:extended): OSErr; var theError : OSErr; begin theError := AEPutParamPtr(outReply, keyDirectObject, typeExtended, Ptr(@inValue), sizeof(extended)); ReturnExtendedProperty := theError; end; function ReturnRectProperty(var outReply:AppleEvent; inRect:Rect): OSErr; var theError : OSErr; begin theError := AEPutParamPtr(outReply, keyDirectObject, typeRectangle, Ptr(@inRect), sizeof(Rect)); ReturnRectProperty := theError; end; function ReturnPointProperty(var outReply:AppleEvent; inPt:Point): OSErr; var theError : OSErr; begin theError := AEPutParamPtr(outReply, keyDirectObject, typeQDPoint, Ptr(@inPt), sizeof(point)); ReturnPointProperty := theError; end; end.