Scripting Features Using the DoScript method 29
Set myInCopy = CreateObject("InCopy.Application")
Rem Set to 7.0 DOM myInCopy.ScriptPreferences.Version = 7.0
Using the DoScript method
The DoScript method gives a script a way to execute another script. The script can be a string of valid scripting code or a file on disk. The script can be in the same scripting language as the current script or another scripting language. The available languages vary by platform: on Mac OS, you can run either an
AppleScript or a JavaScript; on Windows, you can run a VBScript or a JavaScript.
The
DoScript
method has many possible uses:
Running a script in another language that provides a feature missing in your main scripting language.
For example, VBScript lacks the ability to display a file or folder browser, which JavaScript has.
AppleScript can be very slow to compute trigonometric functions (sine and cosine), but JavaScript performs these calculations rapidly. JavaScript does not have a way to query Microsoft® Excel for the contents of a specific spreadsheet cell, but both AppleScript and VBScript have this capability. In all these examples, the
DoScript
method can execute a snippet of scripting code in another language, to overcome a limitation of the language used for the body of the script.
Creating a script “on the fly.” Your script can create a script (as a string) during its execution, which it can then execute using the
DoScript
method. This is a great way to create a custom dialog or panel based on the contents of the selection or the attributes of objects the script creates.
Embedding scripts in objects. Scripts can use the
DoScript
method to run scripts that were saved as strings in the label property of objects. Using this technique, an object can contain a script that controls its layout properties or updates its content according to certain parameters. Scripts also can be embedded in XML elements as an attribute of the element or as the contents of an element. See
“Running scripts at start-up” on page 30 .
Sending parameters to DoScript
To send a parameter to a script executed by
DoScript
, use the following form (from the
DoScriptParameters tutorial script):
Set myInCopy = CreateObject("InCopy.Application") myJavaScript = "alert(""First argument: "" + arguments[0] + ""\rSecond argument: "" + arguments[1]);" myInCopy.DoScript myJavaScript, idScriptLanguage.idJavascript, Array("Hello from
DoScript", "Your message here.") myVBScript = "msgbox arguments(1), vbOKOnly, ""First argument: "" & arguments(0)" myInCopy.DoScript myVBScript, idScriptLanguage.idVisualBasic, Array("Hello from
DoScript", "Your message here.")
Returning values from DoScript
To return a value from a script executed by DoScript , you can use the ScriptArgs (short for “script arguments”) object of the application. The following script fragment shows how to do this (for the complete script, see the DoScriptReturnValue tutorial script):