Scripting tips

When using the API from a scripting language, keep in mind the following:

  • Variables are typically untyped in the scripting language, but have specific data types in Reflection. If a variable cannot be converted to the appropriate type for the API method, a scripting error will occur.

  • You may find it convenient to store a reference to the Reflection API in a script variable, especially when working with a frameset (because the location of the applet can result in a long reference name). For example, instead of prefixing every API method with something like parent.rightframe.document.IBM3270Applet.getAPI("JSAPI","IBM3270"), you might create a variable called mAPI, and set it to the name of the Reflection applet like this:

     // In JavaScript
         var mAPI = parent.rightframe.document.IBM3270Applet.getAPI("JSAPI","IBM3270");

    or

    ' In VBScript
         Set mAPI = parent.rightframe.document.IBM3270Applet.getAPI("JSAPI","IBM3270")

    If you use this technique, keep in mind the scope of the variable and where in your HTML file the script is located. If you attempt to assign the variable before the applet or API exists, you may get a script error saying that the object has no properties, that the variable is not an object, or you may get a null return value for the API. Also notice the use of the Set statement in the VBScript example; the Set statement is required in VBScript to create a reference to an object.

  • The way you specify non-printing characters, such as escape sequences, control characters, etc., when waiting for strings or transmitting data to the host (for example, using transmitString), depends on your scripting language. If you're using JavaScript, you can generally use octal or hexadecimal notation; for example, the octal value \015 or the hex value \x0D for a carriage return. If you're using VBScript, you should use either a VBScript constant (such as vbCR for the carriage return character) or the Chr function to specify the character's value.

  • Most API methods do not generate specific errors (that is, they don't throw Java exceptions). However, many methods return True or False to indicate the success or failure of the method. You should check the return value of these methods before deciding how your script should proceed.

  • Depending on the scripting language you use, you may or may not be able to trap errors that occur in your script code. In JavaScript, you can trap errors with the Window.onerror event handler, to catch situations in which a JavaScript error occurs. In VBScript, you can trap errors in individual procedures with the On Error Resume Next statement.

    You might want to trap errors in cases where you're not certain that an API method is available; if a method is not available and you try to call it, a trappable error occurs. This is different than the situation described above, in which the method is available but fails to produce the desired result.