SCRIPT PREFERENCES

Since you are interested in using scripts to power-boost Adobe InDesign, one preference you need to be comfortable with is script preferences. Although there may be only a couple of properties for Script Preferences that you will find a need for in your scripts, we’ll take a look at the entire list in this discussion.

USER INTERACTION LEVELS

User interaction levels is one of the most used of all the script preferences properties. In our previous blog (August 24), we saw that it is imperative to have user interaction level for script preferences set to interact with all when creating a custom dialog.

Without this, the custom dialog will fail to open. Don’t believe it? Try running the following Test Script in Script Editor.

Test Script

   tell application "Adobe InDesign CC 2015"
      set user interaction level of script preferences to never interact
      set dialogRef to make dialog with properties {name:"My Test", can cancel:false, label:""}
      tell dialogRef
         tell (make dialog column)
            set rowRef to make static text with properties {static label:"Just Testing"}
         end tell
      end tell
      set userResponse to show dialogRef
   end tell

When you run the script you will notice that nothing seems to happen. If you watch the script run from within Script Editor, you will see in the Result Panel that the script runs and then returns false as the result. No error; just no dialog.

Change the value of user interaction level to interact with all, and the script will execute as expected.

   set user interaction level of script preferences to interact with all

User interaction level can also be set to interact with alerts.

Another place you will want to use user interaction level for script preferences is in having a script open an InDesign document. If an error occurs when opening the document, your script can fail miserably, often with a “Can’t execute script as there is a modal dialog open.” The modal dialog is attempting to display the error, but the user is just left with the “can’t execute the script” error with no reason why. The most common reason an error such as this can be thrown when a script attempts to open a document, is because of a missing type font.

Missing Type Font

The way to get around the missing type font dialog is to precede the open document statement in your script with a statement to store the current setting for the user interaction level in a variable followed by one to set the user interaction level to never interact. After opening the document, use another statement to set user interaction level to its previous level.

   set docAlias to choose file with prompt "Choose InDesign document to open"
   tell application "Adobe InDesign CC 2015"
      set origLevel to user interaction level of script preferences
      set userinteraction level of script preferences to never interact
      set docRef to open docAlias
      set user interaction level of script preferences to origLevel
      --other statements to work with the opened document
   end tell 

How your script handles the missing type font issue is another matter. You may just want to make sure that text with missing fonts is highlighted.

   tell application "Adobe InDesign CC 2015
      tell text preferences
         set highlight substituted fonts to true
      end tell
   end tell  

You will also want your script to test to make sure that the file chosen by the user is a valid Adobe InDesign file.

MEASUREMENT UNIT

Top on my list of the most used script preference property is measurement unit. Imagine that your script needs to add a value (in points) to the value returned for some measurement in a document. This could be a dimension returned by a statement such as one that gets the geometric bounds of a selected page item.

   tell application "Adobe InDesign CC 2015"
      set selList to selection
      if selList is not {}
         set selItem to item 1 of selList
         set gBounds to geometric bounds of selItem
      end if
   end tell

If the user has the measurement units for the document set to inches, the result may be a list of numbers such as {0.5, 1.5, 4.02, 8.0}.

A script could set the appropriate measurement unit of the document’s view preferences to points, perform the calculation, and reset the view preferences back to its original value when through. This may or may not work consistently with documents within the current version of InDesign (2015.1 Release).

You may wish to test this out with your version of InDesign.

For the following test, create a document with measurements set to inches. Place a page item on the first page of the document with its top edge flush to the top of the page. With the page item selected, run the following in Script Editor:

Using View Preferences

   tell application "Adobe InDesign CC 2015" --substitute your version here
      tell document 1
         set selList to selection
         set selItem to item 1 of selList
         --place original value in variable
         set origVM to vertical measurement units of view preferences
         --set the measurement unit to points
         set vertical measurement units of view preferences to points
         --get the geometric bounds of the selected item
         set gBounds to geometric bounds of selItem
         --place the top vertical coordinate of the bounds into a variable
         set oldTop to item 1 of gBounds
         --set a new variable to this value as points
         set newTop to "" & (72 + oldTop) & " pts"
         --draw a graphic line using the newTop variable for the top vertical coordinate
         make graphic line with properties {geometric bounds:{newTop, "72 pts", newTop, "400 pts"}, stroke weight:"10 pts", stroke color:"Black"}
         --reset measurement units back to previous value
         set vertical measurement units of view preferences to origVM
      end tell
   end tell
   --for testing
   origVM

In recent tests with release 2015.1, the script did not work as anticipated in one instance. This may be because the subject document was first saved in a previous version of InDesign. To prevent this potential problem from occurring and getting rid of all of the other hassle involved in fiddling with view preferences, use the measurement unit of script preferences instead. It’s beautiful. And it works consistently.

Using Script Preferences

   tell application "Adobe InDesign CC 2015"
      set origPref to measurement unit of script preferences
      set measurement unit of script preferences to points
      tell document 1
         set selList to selection
         set selItem to item 1 of selList
         set gBounds to geometric bounds of selItem
         set newTop to (item 1 of gBounds) + 72
         make graphic line with properties {geometric bounds:{newTop, "72 pts", newTop, "400 pts"}, stroke weight:"10 pts", stroke color:"Black"}
      end tell
      set measurement unit of script preferences to origPref
   end tell 

Saving and restoring the original value of the measurement unit for script preferences may not be required. But, just in case the script might be run on different machines having different script preferences set for the application, it’s always a good idea to restore preferences to the way we found them.

Note: The script above will only work on the first page of the document. For any other page, you need to tell the page, not the document, to create the graphic line.

SCRIPTS FOLDER

The scripts folder property of script preference returns the path to the user’s Scripts Panel folder for the application.

There are two places on the computer where scripts for Adobe InDesign are saved.

  1. Scripts Panel inside the application’s folder
  2. In the Scripts Panel inside the Library folder for the user

For users not having administrator privileges for the application, the user’s Scripts Panel will need to be used. Of course, there are other reasons why you may prefer to use the user’s Scripts folder.

In case you ever are at odds to remember just exactly where the user’s Scripts Panel folder is located, run the following script from inside Script Editor. Script Editor’s Result panel will show the path for you.

Scripts Folder

   tell application "Adobe InDesign CC 2015"
      tell script preferences
         set theTest to scripts folder
      end tell
   end tell
   theTest 

SCRIPTS LIST

The scripts list property of script preferences returns a list of the all the scripts available for InDesign. And, by all, that means literally all, including the “hidden” scripts used by the application to perform some of its magic.

   tell application "Adobe InDesign CC 2015"
      tell script preferences
         set theTest to scripts list
      end tell
   end tell
   theTest

If you only want a listing of the scripts you have in your user’s Script Panel. Use the path to the scripts folder with a list folder statement.

List Folder

   tell application "Adobe InDesign CC 2015"
      tell script preferences
         set scriptPath to (scripts folder) as string
      end tell
      set scriptList to list folder scriptPath without invisibles
   end tell
   scriptList

ENABLE REDRAW

The last property of script preference you may find useful in a script is enable redraw. If you have a script that is performing a wealth of statements that involve drawing the screen, you may want to set this property to false in order to speed up execution.

Redraw

   tell application "Adobe InDesign CC 2015"
      tell enable redraw of script preferences to false
      --rest of script that involves screen redraws
   end tell 

Of course, if you want to view the script actions as the script progresses, you will want to make sure this property is set to true.

Occasionally, setting enable redraw to false can help prevent a number of unsuspected bugs from cropping up as your script progresses. One place I have found this particularly useful is in updating a complex table.

ONWARD AND UPWARD

One functionality we had intended to add to this blog was a script to search for a missing font and replace it with a predefined font. To date, our tests with this script indicate that the functionality using find and change with a type font may be broken. More testing is required.