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

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

Archive