Important: The information in this document is obsolete and should not be used for new development.
Modifying and Recompiling a Compiled Script
In addition to loading and executing a previously compiled and saved script as described in the previous section, your application can use the scripting component routines described in this section to decompile a compiled script, display the equivalent source data to users for editing, and recompile the source data after editing is completed. For example, if a user wants to change the script shown in Figure 7-4 on page 7-10 so that it refers to some other database or looks up other information in addition to the customer's address, the forms application can use scripting component routines to display the compiled script to the user and recompile it after the user has modified it.You can use the
OSAGetSourcefunction to obtain the source data for a compiled script. TheOSAGetSourcefunction takes a component instance, a script ID for the compiled script, and the desired type of the resulting descriptor record. If you specify a component instance that identifies a connection with the generic scripting component, you can useOSAGetSourceto get the source data for any compiled script created by a scripting component that is registered with the Component Manager on the local computer. If you specify a component instance that identifies an explicit connection with a scripting component, you can useOSAGetSourceonly to get the source data for scripts that were compiled by that scripting component.The
MyEditGenericScriptprocedure in Listing 10-5 shows how you can use theOSAGetSourcefunction with a component instance that identifies a connection to the generic scripting component. TheMyEditGenericScriptfunction gets the source data for a compiled script, allows the user to edit it, and recompiles the script so the original script ID refers to the recompiled script data.Listing 10-5 A routine that displays a compiled script for editing and recompiles it
PROCEDURE MyEditGenericScript (scriptID: OSAID); VAR scriptText: AEDesc; myOSAErr: OSAError; ignoreErr: OSErr; BEGIN {first get the source data} myOSAErr := OSAGetSource(gScriptingComponent, scriptID, typeChar, scriptText); {call the application's primitive text editor} MyEditText(scriptText.dataHandle); {now compile the edited script data in scriptText using } { the scripting component that originally created it; } { passing the original script ID to OSACompile causes } { OSACompile to replace the original script with the new one} myOSAErr := OSACompile(gScriptingComponent, scriptText, kOSAModeNull, scriptID); ignoreErr := AEDisposeDesc(scriptText); IF myOSAErr = errOSAScriptError THEN MyGetScriptErrorInfo; END;After obtaining the source data for the script, theMyEditGenericScriptprocedure calls theMyEditTextfunction, which displays the application's own primitive text editor and allows the user to edit the source data. After the user has finished editing the script,MyEditGenericScriptpasses the edited text and the script ID for the original compiled script to theOSACompilefunction, which updates the script ID so that it refers to the modified and recompiled script. ThekOSAModeNullconstant passed in the third parameter ofOSACompileindicates that no mode flags are specified for compilation.If the
OSACompilefunction returnserrOSAScriptError, theMyEditGenericScriptprocedure calls theMyGetScriptErrorInfoprocedure shown in Listing 10-3 on page 10-11 to obtain information about the error.After script data has changed as shown in Listing 10-5, your application should save the modified script data. Listing 10-6 shows how this could be done from a function that loads script data, calls the
MyEditGenericScriptprocedure shown in Listing 10-5 to modify and recompile the script, then saves the modified script data.Listing 10-6 A function that loads and modifies script data, then saves it using a generic storage descriptor record
FUNCTION MyLoadAndModifyScriptData (resourceID: Integer) : OSAError; VAR scriptDesc: AEDesc; storageDescRec: AEDesc; scriptID: OSAID; myOSAErr: OSAError; ignoreErr: OSErr; myHndl: Handle; BEGIN scriptDesc.descriptorType := typeOSAGenericStorage; scriptDesc.dataHandle := GetResource(kOSAScriptResourceType, resourceID); myOSAErr := OSALoad(gGenericScriptingComponent, scriptDesc, kOSAModeNull, scriptID); MyEditGenericScript (scriptID); myOSAErr := OSAStore(gScriptingComponent, scriptID, typeOSAGenericStorage, kOSAModeNull, storageDescRec); MyWriteResource(storageDescRec.dataHandle, resourceID); ignoreErr := AEDisposeDesc(scriptDesc); ignoreErr := AEDisposeDesc(storageDescRec); END;