Adobe 65009333 Scripting Guide - Page 22

Sending parameters to doScript, Returning values from doScript

Page 22 highlights

Scripting Features Using the doScript method 22 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 23. Sending parameters to doScript To send a parameter to a script executed by doScript, use the following form (from the DoScriptParameters tutorial script): var myParameters = ["Hello from DoScript", "Your message here."]; var myJavaScript = "alert(\"First argument: \" + arguments[0] + \"\\rSecond argument: \" + arguments[1]);"; app.doScript(myJavaScript, ScriptLanguage.javascript, myParameters); if(File.fs == "Windows"){ var myVBScript = "msgbox arguments(1), vbOKOnly, \"First argument: \" & arguments(0)"; app.doScript(myVBScript, ScriptLanguage.visualBasic, myParameters); } else{ var myAppleScript = "tell application \"Adobe InCopy CS4\\rdisplay dialog(\"First argument\" & item 1 of arguments & return & \"Second argument: \" & item 2 of arguments & return & end tell"; app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage, myParameters); } 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):

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

Scripting Features
Using the doScript method
22
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 23
.
Sending parameters to doScript
To send a parameter to a script executed by
doScript
, use the following form (from the
DoScriptParameters tutorial script):
var myParameters = ["Hello from DoScript", "Your message here."];
var myJavaScript = "alert(\"First argument: \" + arguments[0] + \"\\rSecond argument:
\" + arguments[1]);";
app.doScript(myJavaScript, ScriptLanguage.javascript, myParameters);
if(File.fs == "Windows"){
var myVBScript = "msgbox arguments(1), vbOKOnly, \"First argument: \" &
arguments(0)";
app.doScript(myVBScript, ScriptLanguage.visualBasic, myParameters);
}
else{
var myAppleScript = "tell application \"Adobe InCopy CS4\\rdisplay dialog(\"First
argument\" & item 1 of arguments & return & \"Second argument: \" & item 2 of arguments
& return & end tell";
app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage, myParameters);
}
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):