AUTOMATING DOCUMENT CREATION

In our previous blog post we started a script that would automate the creation of a web document using a document preset. As part of the automation the script allows the user to choose a stylesheet from which text (paragraph and character) styles, object styles and table styles are imported.

As part of this automation you may think of additional functionality you might want to add to the script as part of the document creation. In this post, we look at some options.

LOAD COLORS

How color libraries should be handled is a matter of personal preference. Colors are most likely defined as part of an object style so they are loaded as part of the object style import. But should you want additional color sets, they can loaded as part of the automated document creation.

The following example script demonstrates loading colors from a file saved in the Styles folder for InDesign’s Presets folder. You will need a document open for testing.

set colorPrompt to "Choose file for colors"
tell application "Adobe InDesign CC 2018"
   set docRef to active document
   set appPath to file path as string
   set stylePath to appPath & "Presets:Styles"
   set styleFiles to list folder colorPath without invisibles
   try
	set itemChoice to choose from list styleFiles with prompt colorPrompt
	if itemChoice is not false then
		set colorFile to item 1 of itemChoice
	else
		error "Requires choice of file to continue"
	end if
	set wasSuccess to my loadSwatches(docRef, stylePath, colorFile)
   on error errStr
	activate
	display alert "Error: " & errStr
   end try
end tell
colorPath

LAYERS

For the most part, InDesign documents should be organized using layers. Background objects are placed on a “Background” layer, images on an “Image” layer, and so on. It makes sense then to allow a document creation script to give the user the option for adding layers.

Creating a Layer

When you create a new layer, by default it is placed at the top of the layer list and becomes the active layer.

A script can refer to a layer by name, by its index in the layer list, or as the current active layer. This is demonstrated in the following:

set layerName to "myNewLayer"
tell application "Adobe InDesign CC 2018"
   tell document 1
	if not (exists layer layerName) then
	   set myLayer to make layer with properties {name:layerName}
	else
	   set myLayer to layer layerName
	end if
	set layerTest to name of active layer
   end tell
end tell
layerTest

The problem with this behavior (new layers placed at the top) is that when you add a layer it normally is for images, or a background which should be placed at the bottom. A script can move the layer being added to the bottom of the existing layers using an index value of -1 to access the bottom layer in the list.

Add Background Layer

(*assumes active document*)
set layerName to "Background"
tell application "Adobe InDesign CC 2018"
   tell document 1
      if not (exists layer layerName) then
	  set myLayer to make layer with properties {name:layerName}
      else
	  set myLayer to layer layerName
      end if
      move myLayer to (after layer -1)
      set active layer to layer 1
   end tell
end tell 

Notice here that the script also sets the active layer to the top layer (layer 1).

If creating a number of layers, you may decide to create them in the reverse order for which you want them in the layer list.

Create multiple layers

(*assumes active document*)
set layerList to {"DieLine", "Background", "Images", "Animation", "Text"}
tell application "Adobe InDesign CC 2018"
   set docRef to document 1
   tell docRef
     repeat with i from 1 to length of layerList
	set layerName to item i of layerList
	if not (exists layer layerName) then
           set myLayer to make layer with properties {name:layerName}
	end if
     end repeat
   end tell
end tell

Of course, this leaves the existing Layer 1 to be the bottom layer. To avoid this, the script could rename the top layer before adding the rest of the layer list items.

The following script creates a custom dialog from which the user can select the document preset for creating the document and indicate which layers will need to be added if any. It also introduces a repeat method for creating checkbox controls in a custom dialog.

Web Doc From Preset with Dialog

set layerList to {"DieLine", "Background", "Images", "Animation", "Text"}
set dlgName to "Document Setup"
tell application "Adobe InDesign CC 2018"
   set presetNames to name of document presets where intent is web intent
end tell
try
   set userResponse to userDialog(dlgName, true, presetNames, layerList)
   set presetIndex to item 1 of userResponse
   set presetName to item presetIndex of presetNames
   set layerNames to item 2 of userResponse
   set docRef to docFromPreset(presetName)
   if layerNames is not {} then
	createLayers(docRef, layerNames)
   end if
on error errStr
   activate
   display alert "Error: " & errStr
end try

(*Creates web document using named preset*)
on docFromPreset(presetName)
   tell application "Adobe InDesign CC 2018"
	set presetRef to document preset presetName
	set docRef to make document with properties {document preset:presetRef}
   end tell
   return docRef
end docFromPreset

(*Creates layers with names as listed in layerList. 
The existing layer 1 is renamed using item 1 of the layerList as part of the process*)
on createLayers(docRef, layerList)
   tell application "Adobe InDesign CC 2018"
	tell docRef
	   if length of layerList > 1 then
		set name of layer 1 to item 1 of layerList
		set layerList to rest of layerList
	   end if
	   repeat with i from 1 to length of layerList
		set layerName to item i of layerList
		if not (exists layer layerName) then
		   make layer with properties {name:layerName}
		end if
	   end repeat
	end tell
   end tell
end createLayers

(*Custom dialog allows user to determine document preset for document
and layers to be created*)
on userDialog(dlgName, cancelIt, presetList, layerList)
   tell application "Adobe InDesign CC 2018"
	set origLevel to user interaction level of script preferences
	set user interaction level of script preferences to interact with all
	set dlgRef to make dialog with properties {name:dlgName, canCancel:cancelIt}
	set checkObjects to {}--object list for checkbox controls
	tell dlgRef
	   tell (make dialog column)
		tell (make dialog row)
		   make static text with properties {static label:"Choose Preset for document"}
		   set filenameDrop to make dropdown with properties {string list:presetList, selected index:0, min width:144}
		end tell
		set enable1 to make enabling group with properties {static label:"Add Layers", checked state:false}
		tell enable1
		   tell (make dialog column)
			tell (make dialog row)
			   repeat with i from 1 to length of layerList
				set end of checkObjects to make checkbox control with properties {static label:item i of layerList, checked state:false}
			   end repeat
			end tell--row
		   end tell--column
		end tell--enabling group
	   end tell--dialog column
	end tell --dialog
	set userResponse to show dlgRef
	if userResponse = true then
	   set checkList to {}
	   set fileIndex to (selected index of filenameDrop) + 1
	   if checked state of enable1 = true then
		repeat with i from 1 to length of checkObjects
		   if checked state of item i of checkObjects is true then
		      set end of checkList to static label of item i of checkObjects
		   end if
		end repeat
	   end if
	end if
	destroy dlgRef
	set user interaction level of script preferences to origLevel
	--if cancelled, throw error; otherwise return values
        if userResponse = false then error "User cancelled"
        return {fileIndex, checkList}
   end tell --application
end userDialog

…custom dialog created from the script

The amount of code necessary for creating a custom dialog may seen like a lot, but notice how the handler for creating the document from a named document preset (docFromPreset) is now significantly reduced in size.

…Layers created from above dialog />

ONWARD AND UPWARD

The ability to import styles and colors from stylesheets was overlooked in this script for sake of brevity. You might want to tackle this as a challenge. For those who have trouble, be aware that this subject will be discussed in the next blog post when we add some simple animation to the document.

Disclaimer:

Scripts provided are for demonstration and educational purposes. No representation is made as to their accuracy or completeness. Readers are advised to use the code at their own risk.