Your prescription for increased productivity and profitability
In our previous blog we explored the concept of passing arguments to a script written for do script. Although you can send any reasonable number of arguments to a do script, for the sake of simplicity, we limited the number of arguments to one:
AppleScript
set myArgs to {"John"} set myScript to "alert (\"Hello \" + arguments[0]);" tell application "Adobe InDesign CC 2015" do script myScript with arguments myArgs language javascript --myArgs passed to arguments list end tell
ExtendScript
var myArgs = ["john"]; var myScript = 'activate\r'; myScript += 'display alert ("Hello \" & item 1 of arguments)'; app.doScript (myScript, ScriptLanguage.APPLESCRIPT_LANGUAGE, myArgs);
Script arguments are script objects for the application referred to as script args in AppleScript and scriptArgs in JavaScript.
tell application "Adobe InDesign CC 2015" tell script args --create an argument with a name and value set value name "ScriptArgument" value "This is my script argument value" --get the value of the argument by name set myArg to get value name "ScriptArgument" end tell end tell display alert "My argument is " & myArg
For Extendscript the same can be written:
//create an argument with a name and value app.scriptArgs.setValue("ScriptArgument", "This is my script argument value."); //get the value of the argument by name var myArg = app.scriptArgs.getValue("ScriptArgument"); alert ("My argument is " + myArg);
If we convert the above scripts to a string value for do script, we have the following:
Written in AppleScript to run doScript using JavaScript:
tell application "Adobe InDesign CC 2015" --set create script argument with name and value set myScript to "app.scriptArgs.setValue('ScriptArgument',¬ 'This is my script argument value.');" & return --get value of argument and assign to variable set myScript to myScript & ¬ "var myArg = app.scriptArgs.getValue('ScriptArgument');" & return --use the variable set myScript to myScript & "alert ('My argument is ' + myArg);" --run the script in JavaScript do script myScript language javascript --outside of do script; test the value of the argument tell script args set returnValue to get value name "ScriptArgument" end tell end tell returnValue
Written in JavaScript to run do script using AppleScript
var myScript = "tell application \"Adobe InDesign CC 2015\"\r"; myScript += "tell script args\r"; //create a script argument with name and value myScript += "set value name \"ScriptArgument\" value \"This is my script argument value\"\r"; //get value of the argument and assign to variable myScript += "set myArg to get value name \"ScriptArgument\"\r"; //use the argument in the script myScript += "display alert (\"My argument is \" & myArg)\r"; myScript += "end tell\r"; myScript += "end tell\r"; //run the script in AppleScript app.doScript (myScript, ScriptLanguage.APPLESCRIPT_LANGUAGE); //outside the script; test value of ScriptArgument var myArg = app.scriptArgs.getValue("ScriptArgument"); alert ("My argument is " + myArg); myScript
The scripts get a little more complex when using arguments to pass information in and return values from do script.
AppleScript running doScript script using JavaScript
set extValue to {"John", "Bill"} tell application "Adobe InDesign CC 2015" set myScript to "var myMessage = arguments[0] + ' and ' + arguments[1] + ' are friends.';" & return set myScript to myScript & "app.scriptArgs.setValue('ScriptMessage', myMessage);" & return set myScript to myScript & "var myArg = app.scriptArgs.getValue('ScriptMessage');" & return set myScript to myScript & "alert('My argument is ' + myArg);" & return do script myScript with arguments extValue language javascript tell script args set myArg to get value name "ScriptMessage" end tell activate display alert ("My argument is " & myArg) end tell
JavaScript running do script script using AppleScript
var extValue = ["John", "Bob"]; //set the value for doScript argument var myScript = "tell application \"Adobe InDesign CC 2015\"\r"; myScript += "set myMessage to item 1 of arguments & \" and \" & item 2 of arguments & \" are friends.\"\r"; myScript += "tell script args\r"; myScript += "set value name \"ScriptMessage\" value myMessage\r"; myScript += "set myArg to get value name \"ScriptMessage\"\r"; myScript += "display alert (\"My argument is \" & myArg)\r"; myScript += "end tell\r"; myScript += "end tell\r"; app.doScript (myScript, ScriptLanguage.APPLESCRIPT_LANGUAGE, extValue); //outside script; test value of ScriptArgument var myArg = app.scriptArgs.getValue("ScriptArgument"); alert ("My argument is " + myArg);
Now that you are somewhat comfortable with the idea of passing argument values in, and returning values from a do script script you might want to explore some real world applications this skill has to offer.
JavaScript has some handy methods that can be applied to arrays that are missing in AppleScript. One that comes to mind is sort(). And, yes, there are a number of very effective sort routines that can be written in AppleScript, but just for demonstration:
(*Sorts list of strings and returns a comma-delimited string of the items sorted alphabetically*) set extValue to {{"John", "Bill", "Mary"}} tell application "Adobe InDesign CC 2015" set myScript to "var myArray = arguments[0];" & return set myScript to myScript & "sortedArray = myArray.sort();" & return set myScript to myScript & "sortedString = sortedArray.join(', ');" & return set myScript to myScript & "app.scriptArgs.setValue('SortedArray', sortedString);" & return do script myScript with arguments extValue language javascript tell script args set myArg to get value name "SortedArray" end tell end tell
On the other side of the ledger, when you have scripts running on a Macintosh, AppleScript has some capabilities you might want to tap your JavaScripts into:
var extList = ["one", "two", "three"]; var myScript = "tell application \"Adobe InDesign CC 2015\"\r"; myScript += "set myList to arguments\r"; //myScript += 'set myList to {\"one\", \"two\", \"three\"}\r' myScript += "set userResponse to choose from list myList\r"; myScript += "if userResponse = false then\r"; myScript += "set value name \"UserChoice\" value (\"Error: User Cancelled\")\r"; myScript += "else\r"; myScript += "tell script args\r"; myScript += "set value name \"UserChoice\" value (item 1 of userResponse)\r"; myScript += "end tell\r"; myScript += "end if\r" myScript += "end tell\r"; app.doScript (myScript, ScriptLanguage.APPLESCRIPT_LANGUAGE, extList); app.scriptArgs.getValue("UserChoice");
Think about some favorite specialized methods that you can take advantage of now that you have a better idea of how script arguments work.