WORKING WITH TABLE AND CELL STYLES

Now that you are automating the creation of tables using cell styles and table styles, you need to save these styles for use in future projects. The hard part of this subject is where to save the styles. If you have administration privileges, the place might be inside InDesign’s application folder. For this you might want to create a folder named “Styles” inside the “Presets” folder. The file path property of the application returns the path to the application folder. With this, add the path to the “Styles” folder you set up for this purpose. A script would than get access to the files in a “Styles” folder inside the application’s “Presets” folder using the following (change the path for stylePath as required for your path):

APPLICATION PATH

tell application "Adobe InDesign CC 2018"
	set appPath to file path as string
	set stylePath to appPath & "Presets:Styles"
end tell

Another location for saving style sheets might be in the user’s Library folder. To use this location, add a folder named Styles to the user’s library.

LIBRARY PATH

The path to this location can be returned from the following.

set folderPath to path to library folder from user domain as string
set stylePath to folderPath & "Styles"

Once you decide on the location for your style files and have your Styles folder, save document styles into a document that can be saved in your Styles folder. With a document open having table styles you want to save, the following script will import the styles into a new document. This is then saved in the pre-defined Style folder (change this path as needed for the path to your Styles folder):

Create Style File

(*Requires open document with table styles that has been saved*)
set templateName to "TableStyles_Basic.indt"
set folderPath to path to library folder from user domain as string
set stylePath to folderPath & "Styles"
tell application "Adobe InDesign CC 2018"
   set docName to name of document 1 --the source document
   set sourceDoc to document docName
   set sourceDocRef to full name of sourceDoc
   set targetDoc to make document
   tell targetDoc
	import styles format table styles format from sourceDocRef
   end tell
   set targetPath to stylePath & ":" & templateName
   close targetDoc saving in targetPath with force save
end tell

To use a style from a Style document, you can import its styles using the following (change folderPath in the following to match the location for your Styles folder):

Import Table Styles

(*Requires open document for which table styles are to be imported*)
set folderPath to path to library folder from user domain as string
try
   set stylePath to folderPath & "Styles"
   set fileList to list folder stylePath without invisibles
   set userChoice to choose from list fileList
   if userChoice is false then
	error ("Error: User cancelled")
   end if
   set targetPath to (stylePath & ":" & item 1 of userChoice)
   tell application "Adobe InDesign CC 2018"
      tell document 1
	 import styles format table styles format from targetPath
      end tell
   end tell
end try 

Interestingly, when you import table styles, the cell styles in the file are also imported. One imported, you can then mix and match the cell styles to create new table styles as needed. You could use ImDesign’s user interface for this purpose. But for the purpose of introducing custom user dialogs we will create one to allow the user to give the new style a name and indicate the cell styles to be used for each region of the table style.

CUSTOM DIALOGS

Creating a script for a custom dialog can get pretty involved depending on the number and types of fields that need to be used. In the script below, we take advantage of the fact that we have a number of dialog elements that are the same with the exception of the label. (The cell style name for each region of the table style to be created can all be chosen from a similar drop down). With this in mind, the sample below uses a repeat loop to create the fields. 

Create Table Style

(*Requires document open having cell styles to be used in creating a table style*)
set labelList to {"Header Region", "Body Region", "Left Column Region", "Right Column Region", "Footer Region"}
try
   tell application "Adobe InDesign CC 2018"
	set docRef to document 1
	set cellStyleList to name of cell styles of docRef
	set tableInfo to my userDialog("Table Style", true, "tableStyle", cellStyleList, labelList)
	set tableStyleRef to my createTableStyle(docRef, tableInfo, cellStyleList)
   end tell
on error errStr
   activate
   display alert ("Error: " & errStr)
end try
tableStyleRef
(*Creates table from tableInfo and list of cell style names
tableInfo has two elements: name for table style and list of indexes
to reference style name in cellStyleList
*)
on createTableStyle(docRef, tableInfo, cellStyleList)
   tell application "Adobe InDesign CC 2018"
	tell docRef
	   set styleName to item 1 of tableInfo
	   if not (exists table style styleName) then
		set thisStyle to make table style with properties {name:item 1 of tableInfo}
	   else
		set doOver to my getYesNo("Style styleName exists, do you want to write over?")
		if doOver then
		   set thisStyle to table style styleName
		else
		   error ("Style exists; user cancelled")
		end if
	   end if
	   copy item 2 of tableInfo to {headIndex, bodyIndex, leftColIndex, rightColIndex, footerIndex}
	   tell thisStyle
		if headIndex is not 1 then
		   set header region cell style to cell style (item headIndex of cellStyleList) of docRef
		end if
		set body region cell style to cell style (item bodyIndex of cellStyleList) of docRef
		if leftColIndex is not 1 then
		   set left column region cell style to cell style (item leftColIndex of cellStyleList) of docRef
		end if
		if rightColIndex is not 1 then
		   set right column region cell style to cell style (item rightColIndex of cellStyleList) of docRef
		end if
		if footerIndex is not 1 then
		   set footer region cell style to cell style (item footerIndex of cellStyleList) of docRef
		end if
	   end tell --table style
	end tell--document
   end tell--application
   return thisStyle
end createTableStyle
(*returns true or false for user's response to yes no prompt*)
on getYesNo(thePrompt)
   activate
   set userResponse to display dialog thePrompt buttons {"Yes", "No"}
   set theBool to button returned of userResponse is "Yes"
   return theBool
end getYesNo
(*Custom dialog returns name and cell style index choice for styles in styleList passed*)
on userDialog(dlgName, cancelIt, dlgLabel, styleList, labelList)
   tell application "Adobe InDesign CC 2018"
      activate
      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, label:dlgLabel}
      tell dlgRef
	 tell (make dialog column)
	    tell (make dialog row)
		make static text with properties {static label:"Table Style Name:"}
		   set nameField to make text editbox with properties {width:144}
	    end tell --row
	    set fieldObjects to {}
	    tell (make dialog row)
		set col1 to (make dialog column)
		set col2 to (make dialog column)
		repeat with i from 1 to length of labelList
		   tell col1
		      make static text with properties {static label:(item i of labelList)}
		   end tell
		   tell col2
		      set end of fieldObjects to make dropdown with properties {min width:144, string list:styleList, selected index:0}
		   end tell
		end repeat
	     end tell --dialog row				
	end tell --column
      end tell --dialog		
      set userResponse to show dlgRef
      if userResponse = true then
	  set styleChoiceList to {}
	  set styleName to edit contents of nameField
	  repeat with i from 1 to length of fieldObjects
	     set thisField to item i of fieldObjects
	     set end of styleChoiceList to (selected index of thisField) + 1
	  end repeat
      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 {styleName, styleChoiceList}
   end tell --application
end userDialog

The top portion of the script is straight-forward. It simply gets a list of cell style names from the active document and passes this to a handler that allows the user to determine the name for the table style and the cell styles for use for each of its regions. This information is then passed to another handler that creates the table style. All of this is inside of a try/on error/end try statement block that will catch any errors created (if user cancels from the dialog, etc.). The handlers can be a little confusing, especially the userDialog(). But don’t panic! We will be covering custom dialogs in a series of its own. For now just read through the script. Add it to your list of usable scripts and see how having styles saved in a Style file can make working with tables—and creating table styles—so much easier.

ONWARD AND UPWARD

So far we have only been working with basic table styles. You can expand on what you have learned so far to create tables that are admittedly much more complex than these.