FINISHING THE CUSTOM DIALOG

In our previous blog we started the process of creating a custom dialog for Adobe InDesign CC 2015. For this we created our dialog with a text editbox and a dropdown to get the name of the PDF file and the PDF preset to use as part of the PDF export. If you were successful in getting the first part of the dialog to work correctly, congratulations. You are now ready to take on two other dialog widgets provided by InDesign for its dialog object: a checkbox control and an enabling group.

CHECKBOX CONTROL

The checkbox control is perhaps the easiest widget to add to a dialog in that it can only have two properties: a static label and checked state. The value for checked state is true if the checkbox control is checked.

Create user interface widget

Add the following to the script created with last week’s blog. Place it directly below the tell/end tell statement block that creates the dropdown.

set securityCheck to make checkbox control with properties {static label: "Add Security", checked state: false}

Place value from widget’s field in Variable

In the part of the dialog code where the values of the widget fields are placed in variables, add:

   set doSecurity to checked state of securityField

Return value stored in variable to main script

For the return statement at the bottom of the handler, change the statement to read:

   return {fileName, presetIndex, doSecurity}

Create variables to receive returned values in call to handler

Lastly, at the top of the script, change the line that calls the dialog to read as follows:

   set {fileName, presetIndex, doSecurity} to customDialog(dialogName)

Make sure your script returns values as anticipated.

Test your script to make sure it compiles and runs as expected. The values returned should display in a list in Script Editor’s Result panel. Once verified, you can add more fields to your dialog using the same pattern as above.

ENABLING GROUP

Now that you are familiar with the checkbox control, we will look at a more complex widget that incorporates a checkbox as part of its interface: the Enabling Group

The enabling group widget makes it possible to group a number of widgets together which can all be made available (or unavailable) as a group for use by the user. If the checkbox at the top of the enabling group is not checked, all of the items included in the group are grayed out and unavailable.

To allow the user to add a watermark to the file as part of the PDF export, there are a number of properties whose values can be determined by the user. This is an ideal role for an enabling group. If a watermark is to be added to the PDF export, the user first checks the checkbox for the enabling group and then responds to its enclosed widget fields.

The code you will be using to create your Watermark enabling group depends on the settings you want to expose to your user. These can include any of the following. The widget to use for each is suggested. 

  • watermark text; text editbox or drop down
  • watermark do print; checkbox control
  • watermark draw in back; checkbox control
  • watermark font color; drop down of available color names
  • watermark font family; dropdown of limited number of possible font family names
  • watermark font style; dropdown of possible font styles for font families listed
  • watermark font point size; integer edit box
  • watermark horizontal offset; measurement editbox
  • watermark vertical offset; measurement editbox
  • watermark horizontal position; dropdown of possible choices
  • watermark vertical position; dropdown of possible choices
  • watermark opacity; percentage editbox
  • watermark rotation; dropdown of common rotation values (0, 45, 90, 170, etc)
  • watermark visibility; checkbox control

Create user interface widget

For demonstration, our watermark enabling group will include a text editbox and two checkbox controls. The code for this will be placed immediately after the code that creates the securityCheck checkbox control.

tell enableGroup
   tell (make dialog column)
	tell (make dialog row)
	    make static text with properties {static label:"Watermark Text:", min width:72}
	    set textField to make text editbox with properties {edit contents:"", min width:180}
	end tell
	set printCheck to make checkbox control with properties {static label:"Do Print", checked state:false}
	set visibleCheck to make checkbox control with properties {static label:"Visibility", checked state:true}
    end tell
end tell

As you can see, the code for creating an enabling group can get to be quite lengthy.

Place value from widget’s field in variable

The information returned from a enabling group can include a number of values. To hold this information, we can set up a list data type variable. If no information is returned from the enabling group, the list will be empty. If the enabling group is checked, the list will be populated with the results of the widget fields.

After the code that sets the value for the doSecurity variable, add the following:

   set enableList to {}
   if checked state of enableGroup is true then
      set end of enableList to edit contents of textField
      set end of enableList to checked state of printCheck
      set end of enableList to checked state of visibleCheck
   end if 

Return value stored in variable to main script

With the values for the enabling group widgets stored in a list, the return statement now needs to include the enableList variable.

   return {fileName, presetIndex, doSecurity, enableList}

THE FINAL HANDLER

The customDialog handler code should now read as follows:

on customDialog(dialogName)
   tell application "Adobe InDesign CC 2015"
	activate
	set origLevel to user interaction level of script preferences
	set dlgRef to make dialog with properties {name:dialogName, can cancel:true, label:""}
	   tell dlgRef
		set colRef to make dialog column
		tell colRef
		   --add fields for collecting information here
		   tell (make dialog row)
			make static text with properties {static label:"File Name:", min width:100}
			set nameField to make text editbox with properties {min width:200, edit contents:""}
		   end tell
		   tell (make dialog row)
			make static text with properties {static label:"PDF Export Preset", min width:100}
			set presetField to make dropdown with properties {string list:presetList, min width:200, selected index:0}
		   end tell
		   set securityCheck to make checkbox control with properties {static label:"Add Security", checked state:false}
		   set enableGroup to make enabling group with properties {static label:"Add Watermark", min width:300, checked state:false}
		   tell enableGroup
			tell (make dialog column)
			tell (make dialog row)
			   make static text with properties {static label:"Watermark Text:", min width:72}
			   set textField to make text editbox with properties {edit contents:"", min width:180}
			end tell
			set printCheck to make checkbox control with properties {static label:"Do Print", checked state:false}
			set visibleCheck to make checkbox control with properties {static label:"Visibility", checked state:true}
		   end tell
		end tell	
	end tell
   end tell
   set willContinue to show dlgRef
	if willContinue then
	   set fileName to edit contents of nameField
	   set presetIndex to (selected index of presetField) + 1
	   set doSecurity to checked state of securityCheck
	   set enableList to {}
	   if checked state of enableGroup is true then
		set end of enableList to edit contents of textField
		set end of enableList to checked state of printCheck
		set end of enableList to checked state of visibleCheck
	   end if
	end if
	destroy dlgRef
	set user interaction level of script preferences to origLevel
	if not willContinue then error "User Cancelled"
	return {fileName, presetIndex, doSecurity, enableList}
   end tell --application
end customDialog 

Even with the comments removed (which we did for the sake of brevity) this code has become quite lengthy and complex.

Test the script

When compiled and run, your dialog should look like the screen capture below.

no space between checkbox and enabling group

Notice in the screen capture above, how close the ckeckbox widget is to the enabling group. To make your dialog look more pleasing, add an empty row between the two widgets.

   make dialog row with properties {static label:""}

Compare the two screen captures, one with the enabling group checked, and one without. Notice how the widgets in the enabling group appear when its checkbox is checked.

row added between checkbox and enabling group

CALL HANDLER PLACING RETURNED VALUES IN VARIABLES

There is one more thing we need to add to the top of our script to make it more user friendly: with timeout. This statement block allows the script to give the user more time to respond to the dialog than is standard for a modal dialog (2 minutes).

You start a timeout block with a statement that sets the maximum time allowed (in seconds). The timeout block is then ended appropriately with an end timeout statement.

   with timeout of 300 seconds
      --time consumptive statements
   end timeout

To receive the information returned and place the values in variables, the top of your script should read similar to the following:

property presetList : missing value
set dialogName to "Export to PDF"
   try
	tell application "Adobe InDesign CC 2015"
		set presetList to name of PDF export presets
	end tell
        with timeout of 300 seconds --five minutes 
	   set {fileName, presetIndex, doSecurity, enableList} to customDialog(dialogName)
        end timeout
        set presetName to item presetIndex of presetList
	if enableList is not {} then
		copy enableList to {wMarkText, wMarkPrint, wMarkVisible}
	end if
   on error errStr number errorNum
        if errorNum is -1712 then  --traps for timeout error
            set errStr to "Timeout error"
        end if
        activate
	display alert errStr
	return
   end try
   {fileName, presetName, doSecurity, enableList}

Notice how the on error statement was changed to detect a timeout error if it occurs.

Whew! That’s a lot of code for a little information. But you now have the means to receive the settings from your user for a script that will save you and your user mountains of time each time a PDF is to be created, especially if a watermark is to be included.

As to security, the script assumes that if security is used, all security values will be set to true. You may decide to make the securityCheck field an enabling group so the user can enter a value for setting the change security password value for the PDF export preference.

ONWARD AND UPWARD

Now that you have a general idea of how to put a custom dialog together in InDesign, make the dialog your own by adding other widgets. (We will cover the remaining dialog widgets in our next blog.) When satisfied with your dialog, add it to your script for exporting a document to PDF.