home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-11-25 | 24.0 KB | 809 lines | [TEXT/MPS ] |
- { Copyright © 1992 Apple Computer, Inc. All rights reserved.}
-
- { This is a sample program to implement the OSA interface. }
- { In this sample program, the source is the same as the internal compiled form. }
- { So compile and decompile just consists of copying and rename, and this is very unlike other OSA implementation. }
- { However, there is a lot of scripting language which just interprets the source and this sample program can be useful }
-
- UNIT OSASample;
-
- INTERFACE
-
- USES Errors, Notification, Components, Processes, Aliases, AppleEvents, OSA, OSAComp;
-
- FUNCTION Main(VAR params: ComponentParameters;
- storage: Handle): ComponentResult;
-
- IMPLEMENTATION
-
- CONST
- MaxIDSlot = 100;
- MySignature = 'SMPL';
-
- TYPE
- GlobalsHandle = ^GlobalsPtr;
- GlobalsPtr = ^GlobalsRecord;
- GlobalsRecord = RECORD
- gSelf: ComponentInstance;
- errorNumber: OSErr;
- errorDesc: AEDesc;
- scriptIDSlot: ARRAY [1..MaxIDSlot] OF AEDesc;
- END;
-
- PROCEDURE IgnoreOSErr(anErr: OSErr);
- INLINE $548F; { addq #2,sp }
-
- FUNCTION MyClose(globals: GlobalsHandle;
- self: ComponentInstance): ComponentResult;
- FORWARD;
-
- FUNCTION MyCanDo(void: GlobalsHandle;
- selector: INTEGER): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSALoad(globals: GlobalsHandle;
- scriptData: AEDesc;
- modeFlags: LONGINT;
- VAR resultingScriptID: OSAID): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSAStore(globals: GlobalsHandle;
- scriptID: OSAID;
- desiredType: DescType;
- modeFlags: LONGINT;
- VAR resultingScriptData: AEDesc): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSADispose(globals: GlobalsHandle;
- scriptID: OSAID): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSAScriptError(globals: GlobalsHandle;
- selector: OSType;
- desiredType: DescType;
- VAR resultingErrorDescription: AEDesc): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSAExecute(globals: GlobalsHandle;
- compiledScriptID: OSAID;
- contextID: OSAID;
- modeFlags: LONGINT;
- VAR resultingScriptValueID: OSAID): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSAScriptingComponentName(globals: GlobalsHandle;
- VAR resultingScriptingComponentName: AEDesc): OSAError;
- FORWARD;
-
- FUNCTION DoOSACompile(globals: GlobalsHandle;
- sourceData: AEDesc;
- modeFlags: LONGINT;
- VAR resultingCompiledScriptID: OSAID): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSAGetSource(globals: GlobalsHandle;
- scriptID: OSAID;
- desiredType: DescType;
- VAR resultingSourceData: AEDesc): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSACoerceFromDesc(globals: GlobalsHandle;
- scriptData: AEDesc;
- VAR resultingScriptValueID: OSAID): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSACoerceToDesc(globals: GlobalsHandle;
- scriptValueID: OSAID;
- desiredType: DescType;
- VAR result: AEDesc): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSALoadExecute(globals: GlobalsHandle;
- scriptData: AEDesc;
- contextID: OSAID;
- modeFlags: LONGINT;
- VAR resultingScriptValueID: OSAID): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSACompileExecute(globals: GlobalsHandle;
- sourceData: AEDesc;
- contextID: OSAID;
- modeFlags: LONGINT;
- VAR resultingScriptValueID: OSAID): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSADoScript(globals: GlobalsHandle;
- sourceData: AEDesc;
- contextID: OSAID;
- desiredType: DescType;
- modeFlags: LONGINT;
- VAR resultingText: AEDesc): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSAMakeContext(contextName: StringPtr;
- parentContext: OSAID;
- VAR resultingContextID: OSAID): ComponentResult;
- FORWARD;
-
- FUNCTION DoOSADisplay(globals: GlobalsHandle;
- scriptValueID: OSAID;
- desiredType: DescType;
- modeFlags: LONGINT;
- VAR resultingText: AEDesc): ComponentResult;
- FORWARD;
-
- FUNCTION Main(VAR params: ComponentParameters;
- storage: Handle): ComponentResult;
-
- VAR
- globals: GlobalsHandle;
- self: ComponentInstance;
- err: ComponentResult;
- myErrDesc: AEDesc;
-
- BEGIN
- IF params.what < 0 THEN { Negative selectors used for component manager calls }
- CASE (params.what) OF
-
- kComponentOpenSelect:
- BEGIN
- globals := GlobalsHandle(NewHandleClear(sizeof(GlobalsRecord)));
- IF globals = NIL THEN
- err := MemError
- ELSE
- BEGIN
- err := AECreateDesc(typeChar, NIL, 0, myErrDesc);
- IF err = NoErr THEN
- BEGIN
- self := ComponentInstance(params.params[0]);
- SetComponentInstanceStorage(self, Handle(globals));
- WITH globals^^ DO
- BEGIN
- gSelf := self;
- errorDesc := myErrDesc;
- END;
- END
- ELSE
- DisposHandle(Handle(globals));
- END;
- END;
-
- kComponentCloseSelect: err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@MyClose));
-
- kComponentCanDoSelect: err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@MyCanDo));
-
- kComponentVersionSelect: err := 0;
-
- OTHERWISE err := badComponentSelector;
- END { Component Manager calls }
-
- ELSE { Specific component routines called through the component manager }
- BEGIN
- CASE (params.what) OF
- kOSASelectLoad: err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSALoad));
-
- kOSASelectStore: err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSAStore));
-
- kOSASelectExecute:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSAExecute));
-
- kOSASelectDisplay:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSADisplay));
-
- kOSASelectScriptError:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSAScriptError));
-
- kOSASelectDispose:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSADispose));
-
- kOSASelectCompile:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSACompile));
-
- kOSASelectGetSource:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSAGetSource));
-
- kOSASelectScriptingComponentName:
- err := CallComponentFunctionWithStorage(storage, params,
- ComponentRoutine(@DoOSAScriptingComponentName));
-
- kOSASelectCoerceFromDesc:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSACoerceFromDesc));
-
- kOSASelectCoerceToDesc:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSACoerceToDesc));
-
- kOSASelectLoadExecute:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSALoadExecute));
-
- kOSASelectCompileExecute:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSACompileExecute));
-
- kOSASelectDoScript:
- err := CallComponentFunctionWithStorage(storage, params, ComponentRoutine(@DoOSADoScript));
-
- kOSASelectMakeContext: err := CallComponentFunction(params, ComponentRoutine(@DoOSAMakeContext));
-
- OTHERWISE
- BEGIN
- err := badComponentSelector;
- END;
- END;
- END;
- Main := err;
- END;
-
- FUNCTION MyClose(globals: GlobalsHandle;
- self: ComponentInstance): ComponentResult;
-
- VAR
- i: integer;
-
- BEGIN
- HLock(Handle(globals));
- WITH globals^^ DO
- BEGIN
- IgnoreOSErr(AEDisposeDesc(errorDesc));
- FOR i := 1 TO MaxIDSlot DO { dispose all the descriptors in the slots }
- IgnoreOSErr(AEDisposeDesc(scriptIDSlot[i]));
- END;
- HUnLock(Handle(globals));
- MyClose := noErr;
- END;
-
- FUNCTION MyCanDo(void: GlobalsHandle;
- selector: INTEGER): ComponentResult;
-
- BEGIN
- MyCanDo := 1 { Should have gone through every selector that is supported }
- END;
-
- { utility procedures }
-
- PROCEDURE ClearErrorDesc(globals: GlobalsHandle);
- { clean up the error message }
-
- BEGIN
- WITH globals^^ DO
- BEGIN
- errorNumber := NoErr;
- SetHandleSize(errorDesc.dataHandle, 0);
- END;
- END;
-
- FUNCTION TryDoScript(globals: GlobalsHandle;
- scriptData: AEDesc;
- VAR num: Integer): ComponentResult;
- { try to compile the text and return the result }
- { we just try to translate one, two, three etc into number in this simple example }
-
- VAR
- aStr: Str255;
- err: OSErr;
-
- BEGIN
- num := 0;
- IF scriptData.descriptorType = MySignature THEN
- BEGIN
- GetIText(scriptData.dataHandle, aStr);
- UprString(aStr, false);
- IF aStr = 'ONE' THEN
- num := 1
- ELSE IF aStr = 'TWO' THEN
- num := 2
- ELSE IF aStr = 'THREE' THEN
- num := 3
- ELSE IF aStr = 'FOUR' THEN
- num := 4
- ELSE IF aStr = 'FIVE' THEN
- num := 5
- ELSE IF aStr = 'SIX' THEN
- num := 6
- ELSE IF aStr = 'SEVEN' THEN
- num := 7
- ELSE IF aStr = 'EIGHT' THEN
- num := 8
- ELSE IF aStr = 'NINE' THEN
- num := 9
- ELSE IF aStr = 'TEN' THEN num := 10;
- ClearErrorDesc(globals);
- IF num = 0 THEN
- BEGIN
- err := errOSAScriptError;
- aStr := Concat('Cannot understand ', aStr);
- WITH globals^^ DO
- BEGIN
- errorNumber := err;
- IgnoreOSErr(PtrToXHand(@aStr[1], errorDesc.dataHandle, length(aStr)));
- END;
- END
- ELSE
- err := 0;
- END
- ELSE
- err := errOSABadStorageType;
- TryDoScript := err;
- END;
-
- FUNCTION ActualDoScript(globals: GlobalsHandle;
- scriptData: AEDesc;
- VAR result: AEDesc): ComponentResult;
- { execute the script and return a number or set the error result }
-
- VAR
- num: integer;
- err: OSErr;
-
- BEGIN
- err := TryDoScript(globals, scriptData, num);
- IF err = NoErr THEN err := AECreateDesc(typeShortInteger, @num, SizeOf(Integer), result);
- ActualDoScript := err;
- END;
-
- PROCEDURE NukeIt(VAR theDesc: AEDesc);
- { make a null descriptor }
-
- BEGIN
- WITH theDesc DO
- BEGIN
- descriptorType := typeNull;
- dataHandle := NIL;
- END;
- END;
-
- FUNCTION FindScriptIDSlot(globals: GlobalsHandle): longint;
- { search for a empty slot and return the slot ID, return 0 if none is available }
-
- VAR
- i: integer;
-
- BEGIN
- FindScriptIDSlot := 0;
- WITH globals^^ DO
- FOR i := 1 TO MaxIDSlot DO
- IF scriptIDSlot[i].dataHandle = NIL THEN
- BEGIN
- FindScriptIDSlot := i;
- LEAVE;
- END;
- END;
-
- FUNCTION FindSlotAndPutIt(globals: GlobalsHandle;
- theDesc: AEDesc;
- VAR slotID: longint): ComponentResult;
- { put a desc into a slot, if slotID starts as 0 find a new slot, otherwise reuse the slot ID }
-
- VAR
- err: OSErr;
- oldDesc: AEDesc;
-
- BEGIN
- IF theDesc.dataHandle = NIL THEN
- BEGIN { we don't have a descriptor, return 0 }
- slotID := 0;
- err := memFullErr;
- END
- ELSE
- BEGIN
- IF slotID = 0 THEN
- slotID := FindScriptIDSlot(globals) { find an empty slot }
- ELSE
- BEGIN { reuse an existing slot, we first dispose the old content }
- oldDesc := globals^^.ScriptIDSlot[slotID];
- IgnoreOSErr(AEDisposeDesc(oldDesc));
- END;
- IF slotID <> 0 THEN
- BEGIN { now we put the descriptor inot the slot }
- globals^^.ScriptIDSlot[slotID] := theDesc;
- err := NoErr;
- END
- ELSE
- BEGIN
- err := memFullErr; { no slot available, treat it as memory error because in a more realistic program we
- would expand the slot s }
- slotID := 0;
- IgnoreOSErr(AEDisposeDesc(theDesc));
- END;
- END;
- FindSlotAndPutIt := err;
- END;
-
- FUNCTION TextToStxt(textDesc: AEDesc;
- VAR result: AEDesc): OSErr;
- { convert TEXT to styled text }
-
- TYPE
- myStyleRec = RECORD
- scrpNStyles: integer;
- scrpStyle: ScrpSTElement;
- END;
-
- VAR
- err: OSErr;
- theRec: AERecord;
- myStyle: myStyleRec;
-
- BEGIN
- result.dataHandle := NIL;
- myStyle.scrpNStyles := 1;
- WITH myStyle.scrpStyle DO
- BEGIN
- scrpStartChar := 0;
- scrpHeight := 16;
- scrpAscent := 12;
- scrpFont := 1;
- scrpFace := [bold];
- scrpSize := 12;
- scrpColor.red := 0;
- scrpColor.green := 0;
- scrpColor.blue := 0;
- END;
- err := AECreateList(NIL, 0, true, theRec);
- IF err = NoErr THEN
- BEGIN
- err := AEPutKeyPtr(theRec, 'ksty', 'styl', @myStyle, SizeOf(myStyle));
- IF err = NoErr THEN err := AEPutKeyDesc(theRec, 'ktxt', textDesc);
- IF err = NoErr THEN
- BEGIN
- err := AECoerceDesc(theRec, 'STXT', result);
- IgnoreOSErr(AEDisposeDesc(theRec));
- END
- ELSE
- IgnoreOSErr(AEDisposeDesc(theRec));
- END;
- TextToStxt := err;
- END;
-
- { the OSA component procedures }
-
- FUNCTION DoOSALoad(globals: GlobalsHandle;
- scriptData: AEDesc;
- modeFlags: LONGINT;
- VAR resultingScriptID: OSAID): ComponentResult;
- { make a copy, strip the trailer and put it into a slot }
-
- VAR
- err: ComponentResult;
- descCopy: AEDesc;
- itsType: DescType;
-
- BEGIN
- err := errOSABadStorageType;
- IF (scriptData.descriptorType = kOSAGenericScriptingComponentSubtype) THEN
- IF OSAGetStorageType(scriptData.dataHandle, itsType) = NoErr THEN
- IF itsType = MySignature THEN
- BEGIN
- err := AEDuplicateDesc(scriptData, descCopy);
- IF err = NoErr THEN err := OSARemoveStorageType(descCopy.dataHandle);
- descCopy.descriptorType := MySignature; { internally we store as as type 'SMPL' }
- resultingScriptID := 0; { we want a new OSAID }
- err := FindSlotAndPutIt(globals, descCopy, resultingScriptID);
- END;
- DoOSALoad := err;
- END;
-
- FUNCTION DoOSAStore(globals: GlobalsHandle;
- scriptID: OSAID;
- desiredType: DescType;
- modeFlags: LONGINT;
- VAR resultingScriptData: AEDesc): ComponentResult;
- { make a copy of content in the slot and add trailer }
-
- VAR
- err: ComponentResult;
-
- BEGIN
- NukeIt(resultingScriptData);
- IF (scriptID = 0) | (scriptID > MaxIDSlot) THEN
- err := errOSAInvalidID
- ELSE
- BEGIN
- resultingScriptData := globals^^.ScriptIDSlot[scriptID];
- IF resultingScriptData.dataHandle = NIL THEN
- err := errOSAInvalidID
- ELSE
- BEGIN
- err := HandToHand(resultingScriptData.dataHandle);
- IF err <> NoErr THEN NukeIt(resultingScriptData);
- END;
- IF err = NoErr THEN
- BEGIN
- err := OSAAddStorageType(resultingScriptData.dataHandle, resultingScriptData.descriptorType);
- IF err = NoErr THEN
- resultingScriptData.descriptorType := kOSAGenericScriptingComponentSubtype
- ELSE
- IgnoreOSErr(AEDisposeDesc(resultingScriptData));
- END;
- END;
- DoOSAStore := err;
- END;
-
- FUNCTION DoOSAScriptError(globals: GlobalsHandle;
- selector: OSType;
- desiredType: DescType;
- VAR resultingErrorDescription: AEDesc): ComponentResult;
- { fetch the content of the error descriptor }
-
- VAR
- err: ComponentResult;
- i: longint;
- aRec: AERecord;
- myErrorDesc: AEDesc;
- errNum: integer;
-
- BEGIN
- NukeIt(resultingErrorDescription);
- IF selector = kOSAErrorNumber THEN
- BEGIN
- errNum := globals^^.errorNumber;
- err := AECoercePtr(typeShortInteger, @errNum, SizeOf(Integer), desiredType, resultingErrorDescription);
- END
- ELSE IF selector = kOSAErrorMessage THEN
- BEGIN
- myErrorDesc := globals^^.errorDesc;
- IF (desiredType = typeChar) | (desiredType = typeWildCard) THEN
- err := AEDuplicateDesc(myErrorDesc, resultingErrorDescription)
- ELSE IF desiredType = 'STXT' THEN
- err := TextToStxt(myErrorDesc, resultingErrorDescription)
- ELSE
- err := AECoerceDesc(myErrorDesc, desiredType, resultingErrorDescription);
- END
- ELSE IF selector = kOSAErrorRange THEN
- BEGIN { in this simple example, we make the error range to include everything }
- err := AECreateList(NIL, 0, true, aRec);
- IF err = NoErr THEN
- BEGIN
- i := 0;
- IgnoreOSErr(AEPutKeyPtr(aRec, keySourceStart, typeLongInteger, @i, SizeOf(i)));
- i := 30000;
- IgnoreOSErr(AEPutKeyPtr(aRec, keySourceEnd, typeLongInteger, @i, SizeOf(i)));
- IF desiredType = typeAERecord THEN
- resultingErrorDescription := aRec
- ELSE
- BEGIN
- IF desiredType = typeWildCard THEN desiredType := typeOSAErrorRange;
- err := AECoerceDesc(aRec, desiredType, resultingErrorDescription);
- IgnoreOSErr(AEDisposeDesc(aRec));
- END;
- END;
- END
- ELSE
- err := errOSABadSelector;
- DoOSAScriptError := err;
- END;
-
- FUNCTION DoOSADispose(globals: GlobalsHandle;
- scriptID: OSAID): ComponentResult;
- { dispose the descriptor in the slot }
-
- BEGIN
- IF (scriptID > 0) & (scriptID <= MaxIDSlot) THEN
- BEGIN
- HLock(Handle(globals));
- IgnoreOSErr(AEDisposeDesc(globals^^.ScriptIDSlot[scriptID]));
- HUnLock(Handle(globals));
- END;
- DoOSADispose := NoErr;
- END;
-
- FUNCTION DoOSAExecute(globals: GlobalsHandle;
- compiledScriptID: OSAID;
- contextID: OSAID;
- modeFlags: LONGINT;
- VAR resultingScriptValueID: OSAID): ComponentResult;
- { since in this sample program, internal form is the same as source form except for the descriptor type, we can just call CompileExecute }
-
- BEGIN
- resultingScriptValueID := 0;
- IF (compiledScriptID <= 0) | (compiledScriptID > MaxIDSlot) THEN
- DoOSAExecute := errOSAInvalidID
- ELSE
- DoOSAExecute := DoOSACompileExecute(globals, globals^^.ScriptIDSlot[compiledScriptID], contextID, modeFlags,
- resultingScriptValueID);
- END;
-
- FUNCTION DoOSADisplay(globals: GlobalsHandle;
- scriptValueID: OSAID;
- desiredType: DescType;
- modeFlags: LONGINT;
- VAR resultingText: AEDesc): ComponentResult;
- { in this program, we have no special form for display so just coerce it }
-
- BEGIN
- DoOSADisplay := DoOSACoerceToDesc(globals, scriptValueID, desiredType, resultingText);
- END;
-
- FUNCTION DoOSACompile(globals: GlobalsHandle;
- sourceData: AEDesc;
- modeFlags: LONGINT;
- VAR resultingCompiledScriptID: OSAID): ComponentResult;
- { since internal form is same as source, just change the dataType and call TryDoScript, if it compiles then just store it }
-
- VAR
- err: ComponentResult;
- descCopy, oldCopy: AEDesc;
- num: integer;
-
- BEGIN
- descCopy.dataHandle := NIL;
- oldCopy.dataHandle := NIL;
- IF sourceData.descriptorType = typeChar THEN
- BEGIN
- sourceData.descriptorType := MySignature;
- err := TryDoScript(globals, sourceData, num);
- IF err = NoErr THEN
- BEGIN
- err := AEDuplicateDesc(sourceData, descCopy);
- IF err = NoErr THEN err := FindSlotAndPutIt(globals, descCopy, resultingCompiledScriptID);
- END;
- END
- ELSE
- err := errOSABadStorageType;
- DoOSACompile := err;
- END;
-
- FUNCTION DoOSAGetSource(globals: GlobalsHandle;
- scriptID: OSAID;
- desiredType: DescType;
- VAR resultingSourceData: AEDesc): ComponentResult;
- { in this sample program, source is same as internal form and there is no special formatting, so just call coerce }
-
- BEGIN
- DoOSAGetSource := DoOSACoerceToDesc(globals, scriptID, desiredType, resultingSourceData);
- END;
-
- FUNCTION DoOSACoerceFromDesc(globals: GlobalsHandle;
- scriptData: AEDesc;
- VAR resultingScriptValueID: OSAID): ComponentResult;
- { just store a copy into the slot }
-
- VAR
- err: ComponentResult;
- descCopy: AEDesc;
-
- BEGIN
- err := NoErr;
- descCopy.dataHandle := NIL;
- err := AEDuplicateDesc(scriptData, descCopy);
- IF err = NoErr THEN
- BEGIN
- resultingScriptValueID := 0; { put it in a new slot }
- err := FindSlotAndPutIt(globals, descCopy, resultingScriptValueID);
- END;
- DoOSACoerceFromDesc := err;
- END;
-
- FUNCTION DoOSACoerceToDesc(globals: GlobalsHandle;
- scriptValueID: OSAID;
- desiredType: DescType;
- VAR result: AEDesc): ComponentResult;
- { fetch from the slot and coerce it, if it is source we rename the type because internal form is same as the source text }
-
- VAR
- err: ComponentResult;
- myScriptValue: AEDesc;
-
- BEGIN
- IF (scriptValueID <= 0) | (scriptValueID > MaxIDSlot) THEN
- err := errOSAInvalidID
- ELSE
- BEGIN
- myScriptValue := globals^^.ScriptIDSlot[scriptValueID];
- IF myScriptValue.descriptorType = MySignature THEN myScriptValue.descriptorType := typeChar;
- IF myScriptValue.descriptorType = desiredType THEN
- err := AEDuplicateDesc(myScriptValue, result)
- ELSE IF desiredType = 'STXT' THEN
- err := TextToStxt(myScriptValue, result)
- ELSE
- err := AECoerceDesc(myScriptValue, desiredType, result);
- END;
- DoOSACoerceToDesc := err;
- END;
-
- FUNCTION DoOSALoadExecute(globals: GlobalsHandle;
- scriptData: AEDesc;
- contextID: OSAID;
- modeFlags: LONGINT;
- VAR resultingScriptValueID: OSAID): ComponentResult;
- { strip the trailer, execute it and put back the trailer }
- { there is chance we cannot restore the original form although we are try our best }
- { this really calls for a GetScriptDataSize call in OSAComp }
-
- VAR
- err: ComponentResult;
- itsType: DescType;
-
- BEGIN
- err := errOSABadStorageType;
- IF (scriptData.descriptorType = kOSAGenericScriptingComponentSubtype) THEN
- IF OSAGetStorageType(scriptData.dataHandle, itsType) = NoErr THEN
- IF itsType = MySignature THEN
- BEGIN
- err := OSARemoveStorageType(scriptData.dataHandle);
- IF err = NoErr THEN
- BEGIN
- err := DoOSACompileExecute(globals, scriptData, 0, 0, resultingScriptValueID);
- IF OSAAddStorageType(scriptData.dataHandle, MySignature) <> NoErr THEN
- BEGIN { we are in deep trouble, we change scriptData and cannot put it back }
- IgnoreOSErr(DoOSADispose(globals, resultingScriptValueID)); { dispose result to get back the memory }
- resultingScriptValueID := 0;
- IgnoreOSErr(OSAAddStorageType(scriptData.dataHandle, MySignature)); { now try again }
- err := memFullErr; { fail because we don't have enough memory }
- END;
- END;
- END;
- DoOSALoadExecute := err;
- END;
-
- FUNCTION DoOSACompileExecute(globals: GlobalsHandle;
- sourceData: AEDesc;
- contextID: OSAID;
- modeFlags: LONGINT;
- VAR resultingScriptValueID: OSAID): ComponentResult;
- { since source is same as internal form, just execute it and store the result }
-
- VAR
- err: ComponentResult;
- resultDesc: AEDesc;
-
- BEGIN
- sourceData.descriptorType := MySignature;
- err := ActualDoScript(globals, sourceData, resultDesc);
- IF err = NoErr THEN err := FindSlotAndPutIt(globals, resultDesc, resultingScriptValueID);
- DoOSACompileExecute := err;
- END;
-
- FUNCTION DoOSADoScript(globals: GlobalsHandle;
- sourceData: AEDesc;
- contextID: OSAID;
- desiredType: DescType;
- modeFlags: LONGINT;
- VAR resultingText: AEDesc): ComponentResult;
- { since source is same as internal form, just execute it and return the result }
-
- VAR
- err: ComponentResult;
- resultDesc: AEDesc;
-
- BEGIN
- sourceData.descriptorType := MySignature;
- NukeIt(resultingText);
- err := ActualDoScript(globals, sourceData, resultDesc);
- IF err = NoErr THEN
- BEGIN
- IF (desiredType = resultDesc.descriptorType) | (desiredType = typeWildCard) THEN
- resultingText := resultDesc
- ELSE
- BEGIN
- IF desiredType = 'STXT' THEN
- err := TextToStxt(resultDesc, resultingText)
- ELSE
- err := AECoerceDesc(resultDesc, desiredType, resultingText);
- IgnoreOSErr(AEDisposeDesc(resultDesc));
- END;
- END;
- DoOSADoScript := err;
- END;
-
- FUNCTION DoOSAMakeContext(contextName: StringPtr;
- parentContext: OSAID;
- VAR resultingContextID: OSAID): ComponentResult;
- { context is not used in this sample program }
-
- BEGIN
- resultingContextID := 0;
- DoOSAMakeContext := NoErr;
- END;
-
- FUNCTION DoOSAScriptingComponentName(globals: GlobalsHandle;
- VAR resultingScriptingComponentName: AEDesc): OSAError;
-
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := 'SampleScript';
- DoOSAScriptingComponentName := AECreateDesc(typeChar, @aStr[1], length(aStr), resultingScriptingComponentName);
- END;
-
- END.
-