Automating Adobe InDesign CC 2015


Adobe InDesign do script II

Tuesday, June 07, 2016

Working with Do Script

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);

Using script arguments for Do Script

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

Arguments Passed In and Returned

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);

Real World Application

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:

Sort List

(*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:

User Choice

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");

Onward and Upward

Think about some favorite specialized methods that you can take advantage of now that you have a better idea of how script arguments work.

Disclaimer

Scripts are provided for demonstration purposes only. Users are advised to add error handling as needed.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18153&PostID=1463780&A=Trackback
Trackbacks
Post has no trackbacks.

Recent Posts


Tags

Enabling Groups Adding Captions Library Folder HTML Export PDF export preferences Document from Template lists automated workflow scripting InDesign CC Repeat With text placement Autoflow script folder script templates PDF Read text file as list Document stylesheets text item delimiters functions Find Text Export to PDF placeable reframe active spread text editbox fit do script try/on error Custom Buttons trimText ePub for iPad metacharacters Editbox path save template directory transform if/then file paths wild cards Choose file entire path arrays Fixed Format ePub measurement editboxes page items text concatenation InDesign dialogs Find by Font with timeout measurements Choose from list massage text Character Styles paragraph styles InDesign template Border Panel stylesheets Video say resolve images Custom Dialog BrdgeTalk doScript repeat loops System Events automating InDesign styles Embed Document table styles GPS metadata AppleScript dialogs Tables Custom CSS Style Sheets Index of blogs and scripts for year 2016 Fixed Layout ePub ePub for Kindle Hyperlinks XML tags transformation matrix CALS tables GPS AppleScript image metadata coordinates Preferences records Combobox Find by Attribute new document parent page AppleScript Templates handlers Automating animations CSS Table cells tutorial Numbering object styles watermark Metadata pasteboard Export HTML PDF security folder import text parent Sound Change Text Templates cut and paste Checkbox Control Image Placement Interactive Publications Nested Styles dropdowns Styling Text XML structure enable redraw Publish Online Create Document Glossary Radiobutton Group integer editboxes resize import XML Excel Document Presets Text Cleanup Bullets Apple apps Find by Color Create Text Frame Placing images Find Change Mail import HTML Script Preferences find change options Caption Variable selection External Object Scripts automation transform again user interaction level Media XML Adobe InDesign CC

Archive