DO SCRIPT

There are a number of ways that the do script method (doScript for ExtendScript) in a script for InDesign can be used to an advantage. Here we look at processes that involve a script written in an external file:

  1. Cross Application Scripts
    When you need to pull information from another application into a script written for InDesign, using do script to run the script is often the best way to handle the situation.
  2. Script Library
    You may decide to have a number of scripts each of which perform a specific functionality saved in a protected area of your computer. Any one of these scripts can be called from an InDesign script when needed using do script.
  3. External Script Objects
    There are a number of Adobe processes using BridgeTalk that work with External Script Objects. Access to these need to be written in an external script that can be called using do script.

The automation situations listed above often require that you pass and get back some information from the script file called. This is not as difficult as it may sound, but can be a stumbling block for someone new to writing scripts. For those of you, the following step by step may be helpful.

SAMPLE DO SCRIPT

External Script

  1. Start with a script that is saved somewhere handy on your computer. For this demonstration, the user’s desktop folder is used.
  2. Launch ExtendScript Toolkit, start a new script, and enter the following code:
    alert ("script called");
    var firstArg = arguments[0];
    var nextArg = arguments[1];
    myCalc = firstArg * nextArg;

Notice that the values passed to the external script are always part of an array (list). In the external script, the values passed are in an array (list) identified as arguments. Also, the value to be passed back is not declared or initialized in the external script.

Call From ExtendScript

To call this script and get its result, the following script is written. Again, do this in the ExtendScript editor.

  1. Identify values to be passed (any legal variable identifier can be used)
       var argArray = [123, 2];
  2. Establish a reference to the script to be called.
       var filePath = "~/Desktop/MultiplyIt.jsx";
       var myFile = new File(filePath);
  3. If the file is found, run the script using doScript.
       if (myFile.exists) {
            alert ("file exists");
            //establish variable for return value here
           var myCalc =  app.doScript(myFile, ScriptLanguage.javascript, argArray);
       } else {
            alert ("does not exist");
       }
  4. Display result in JavaScript Console (part of ExtendScript Toolkit). If you do not see the JavaScript Console at the upper right of the script window, make sure the JavaScript Console item is checked in the Window menu.

Notice that the call to doScript above has three arguments: the file reference, the language the script is written in, the argument array containing information required by the external file.

Call using AppleScript

To call the same JavaScript from a script written using AppleScript, the following code can be used.

   set argList to {123, 2}
   set desktopFolder to path to desktop from user domain as string
   set filePath to desktopFolder & "MultiplyIt.jsx"
   if (exists filePath) then
      set myFile to filePath as alias
      tell application "Adobe InDesign CC 2015"
	set myCalc to do script myFile language javascript with arguments argList
      end tell
   else
      activate
      display alert "File does not exist"
   end if
   myCalc

GET FILE METADATA

Our previous blog (June 30) created a script that would return the GPS coordinates from an external file. The code for this targeted a file named Marmot.jpg that was located on the desktop.

   var filePath = "~/desktop/Marmot.jpg";
   var fileRef = new File(filePath);
      if (fileRef instanceof File) {
         if (ExternalObject.AdobeXMPScript == undefined){
           ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
         }
         var xmpf = new XMPFile (fileRef.fsName, XMPConst.NS_PHOTOSHOP, XMPConst.OPEN_FOR_READ);
         var xmp = xmpf.getXMP();
         xmpf.closeFile()
         var myLat = xmp.getProperty(XMPConst.NS_EXIF, "GPSLatitude").toString();
         var myLong = xmp.getProperty(XMPConst.NS_EXIF, "GPSLongitude").toString();
         myString = myLat + ", " + myLong;
      }

Make It Real

Now that you understand how to call an external file using do script (doScript in ExtendScript), you can modify this script to be called from within a script written for InDesign.

  1. Change the first line of the script to read:
       var filePath = arguments[0];
  2. Remembering that all files may not have GPS coordinates, modify the part of the script that defines the variables myLat and myLong to read as follows (you can delete the code that is commented out):
     //var myLat = xmp.getProperty(XMPConst.NS_EXIF, "GPSLatitude").toString();
        //var myLong = xmp.getProperty(XMPConst.NS_EXIF, "GPSLongitude").toString();
        var myLat = "";
        var myLong = "";
        if (xmp.getProperty(XMPConst.NS_EXIF, "GPSLatitude") != undefined) {
            myLat = xmp.getProperty(XMPConst.NS_EXIF, "GPSLatitude").toString();
         }
         if (xmp.getProperty(XMPConst.NS_EXIF, "GPSLongitude") != undefined) {
            myLat = xmp.getProperty(XMPConst.NS_EXIF, "GPSLongitude").toString();
           }
  3. Save the script for testing on the desktop with the name “getGPS.jsx.”

Calling getGPS Script

To call the getGPS.jsx script and get its result back in an AppleScript script, the following can be used.

AppleScript to Call getGPS.jsx

CallgetJPS

   set desktopFolder to path to desktop from user domain as string
   set filePath to desktopFolder & "getGPS.jsx"
   try
      set myFile to filePath as alias
      tell application "Adobe InDesign CC 2015"
	set selList to selection
	set selItem to item 1 of selList
	if class of selItem is rectangle and (count of images of selItem) > 0 then
	   set imageRef to image 1 of selItem
	else if class of selItem is image then
	   set imageRef to selItem
	end if
	set linkRef to item link of imageRef
	set argList to {file path of linkRef}
	set myString to do script myFile language javascript with arguments argList
      end tell
   on error (errStr)
      activate
      display alert errStr
      return
   end try         
   myString

Notice that the reference to the external script in AppleScript needs to be an alias.

Test the Script

Create an InDesign document with a JPEG image placed and selected. Test your CallgetGPS script above. When you are successful in getting the GPS coordinates back as a comma-delimited string, you are ready to use this information in your InDesign document.

Using the Result

The value of the variable myString should now be a comma-delimited string. This will need to be split in order for the individual items in the string to be used. A handler that can be used for this purpose can be written as follows in AppleScript:

   --Handler to split a comma-delimited string to a list
   on stringToList(myString)
	set oldDelim to AppleScript's text item delimiters
	--make sure you include the space after the comma
	set AppleScript's text item delimiters to ", "
	set theList to text items of myString
	set AppleScript's text item delimiters to oldDelim
	return theList
   end stringToList 

Assuming that your script returns a comma-delimited string for the variable myString, you can add the stringToList handler and the following code to your CallgetGPS script above:

   set myList to stringToList(myString)
   display dialog "Latitude is " & item 1 of myList & return & "Longitude is " & item 2 of myList

ON YOUR OWN

Modify the CallgetJPS script to do the following:

  1. Test for values for the list items in the list identified by the variable myList
  2. If values are valid (not empty strings), place a text frame below the image selected and populate it to read the same as the display dialog above.