Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Interapplication Communication /
Chapter 10 - Scripting Components / Using Scripting Component Routines


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 OSAGetSource function to obtain the source data for a compiled script. The OSAGetSource function 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 use OSAGetSource to 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 use OSAGetSource only to get the source data for scripts that were compiled by that scripting component.

The MyEditGenericScript procedure in Listing 10-5 shows how you can use the OSAGetSource function with a component instance that identifies a connection to the generic scripting component. The MyEditGenericScript function 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, the MyEditGenericScript procedure calls the MyEditText function, 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, MyEditGenericScript passes the edited text and the script ID for the original compiled script to the OSACompile function, which updates the script ID so that it refers to the modified and recompiled script. The kOSAModeNull constant passed in the third parameter of OSACompile indicates that no mode flags are specified for compilation.

If the OSACompile function returns errOSAScriptError, the MyEditGenericScript procedure calls the MyGetScriptErrorInfo procedure 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 MyEditGenericScript procedure 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;

Previous Book Contents Book Index Next

© Apple Computer, Inc.
7 JUL 1996