Your prescription for increased productivity and profitability
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:
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.
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.
To call this script and get its result, the following script is written. Again, do this in the ExtendScript editor.
var argArray = [123, 2];
var filePath = "~/Desktop/MultiplyIt.jsx"; var myFile = new File(filePath);
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"); }
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.
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
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; }
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.
var filePath = arguments[0];
//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(); }
To call the getGPS.jsx script and get its result back in an AppleScript script, the following can be used.
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.
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.
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
Modify the CallgetJPS script to do the following: