Your prescription for increased productivity and profitability
There are times when writing a script in one language that you will want to be able to use statements or functionality that is only available to another. Thankfully we have do script which allows different languages to work together. Passing values between the languages is straightforward once you “get it.” Here’s the idea.
When working with functions (handlers in AppleScript) values are passed using parameters:
--the call to the handler passes the values set theResult to doAddition (124, 336) theResult --the handler on doAddition (value1, value2) set theSum to value1 + value2 return theSum end doAddition
Passing values to a script written in a different scripting language is similar. Instead of using a function (or handler), do script is used. To pass values to and from the script an argument list is used.
It may help to think of an argument list as an object that lives outside of both languages. Like an interpreter it passes messages between people who speak two different languages.
To understand this better, some simple examples are used. Recall the steps for using do script:
var myValues = ["value 1", "value 2"]; alert ("first " + myValues[0] + " second " + myValues[1]);
set myValues to {"value 1", "value 2"}
set myJScript to "alert (\"first \" + myValues[0] + \" second \" + myValues[1]);"
The array items myValues[0] and myValues[1] would throw an error on execution as they belong to AppleScript. Instead the values are given to the argument list (arguments) and passed:
set myJScript to "alert (\first \" + arguments[0] + \" second \" + arguments[1]);"
do script myJScript language javascript with arguments myArguments
Notice the variable used in the do script statement (myArguments) is the same one used to identify the values to be passed from AppleScript (myArguments). The script in its entirety reads:
tell application "Adobe InDesign CS5.5" set myValues to {"value 1", "value 2"} set myJScript to "alert (\"First argument: \" + arguments[0] + \"Second argument \" + arguments[1]);" --when the script is run, the value list is passed to the arguments list do script myJScript language javascript with arguments myValues end tell
To pass values from a do script script, the arguments list is also used. Here is an example. Notice the arguments list is identified after the script is created but before the do script statement. The script assumes there is a text frame on the first page of an open document with measurements set to points.
var myValues = ["Text for testing", [36, 36, 200, 300]]; var docRef = app.documents.item(0); var pageRef = docRef.pages.item(0); var textRef = pageRef.textFrames.add({geometricBounds: myValues[1], contents:myValues[0]}); var dupRef = textRef.duplicate();
tell application "Adobe InDesign CS5.5" --the script set up as text set myJScript to "var docRef = app.documents.item(0);" & return set myJScript to myJscript & "var pageRef = docRef.pages.item(0);" & return set myJScript to myJScript & "var textRef = pageRef.textFrames.add({geometricBounds: arguments[1], contents:arguments[0]});" & return set myJScript to myJScript & "dupRef = textRef.duplicate();"& return --set up arguments list set myValues to {"Text for testing", {36, 36, 200, 300}} --call the script defining the value to be returned set dupRef to (do script myJScript language javascript with arguments myValues) set fill color of dupRef to "Black" set fill tint of dupRef to 20 end tell
Notice in the above that the variable dupRef is defined in the call to do script. Inside the script, the same variable is used so the var keyword is not used.
This should give you some food for thought while enjoying some holiday festivities.
Happy Holidays!