Automating Adobe InDesign 2018


Styling Text in Tables

Wednesday, November 14, 2018

Styling Text in Tables

In the previous blog post we saw that the style for the text object that creates a table, sets the styling for imported text that is converted to a table. This can be a great time saver when all text in a table is to be set uniformly.

Style Converted Import

(*Expects insertion point selection in empty text frame
Requires paragraph style "Center" for active document*)
set fileChoice to choose file with prompt "Choose tab-delimited file"
tell application "Adobe InDesign CC 2018"
   set insRef to item 1 of selection
   set frameRef to parent of insRef
   --styling the insertion point
   set applied paragraph style of insRef to paragraph style "Center" of document 1
   tell insRef to place fileChoice
   tell (a reference to text 1 of frameRef)
	set tableRef to convert to table column separator tab row separator return
   end tell
   tell row 1 of tableRef to merge cell 1 with cell -1
end tell

Notice the controlling phrase in the above is imported text converted to a table. This works because the imported text inherits the styling before it is converted to a table.

This also works for tab delimited text selected in a document and with a character style as well:

Style Selection

(*Expects tab-delimited text selection
Requires character style "Bold" in active document.*)
tell application "Adobe InDesign CC 2018"
   set charStyle to character style "Bold" of document 1
   set selRef to selection
   if selRef is not {} and class of item 1 of selRef is text then
	set textRef to object reference of item 1 of selection
	set applied character style of textRef to charStyle
	tell item 1 of selRef
	   set tableRef to convert to table column separator tab row separator return
	end tell
   end if
end tell

With this example the existing paragraph style for the selected text is overridden by the character style "Bold". Of course you can set all text styling in a table as above and then set styling for individual cells as needed.

Style Cell Text

(*Assumes paragraph styles "Head" and "Body" exist for the document
Expects tab-delimited text exists in text frame selected*)
tell application "Adobe InDesign CC 2018"
   set docRef to document 1
   tell docRef
	set frameRef to item 1 of selection
	set headStyle to paragraph style "Head"
	set bodyStyle to paragraph style "Body"
	set textRef to a reference to text 1 of frameRef
	set applied paragraph style of textRef to bodyStyle
	tell textRef
	   set tableRef to convert to table column separator tab row separator return
	end tell
	tell row 1 of tableRef to merge cell 1 with cell -1
	set applied paragraph style of text of cell 1 of tableRef to headStyle
   end tell
end tell

Using Cell Styles

Better yet, with cell styles established for a document, you can not only set text styles for text but any of the many other properties available to a cell style.

In the first post for this series we introduced a handler that creates a cell style if one by the given name does not exist. For this example, we will add a paragraph style to the properties list passed to for the getCellStyle handler.

Get Cell Style

(*Expects paragraph style "Head" in active document
value record returnValues would be returned from custom dialog or other.*)
set returnValues to {paraStyle:"Head", styleName:"HeadCell", swatchName:"Black", colorTint:10}
tell application "Adobe InDesign CC 2018"
   set docRef to document 1
   set myColor to swatch (swatchName of returnValues) of docRef
   set paraStyle to paragraph style (paraStyle of returnValues) of docRef
   set myTint to colorTint of returnValues
   set styleName to styleName of returnValues
   set propList to {applied paragraph style:paraStyle, fill color:myColor, fill tint:myTint}
   set cellStyle to my getCellStyle(docRef, styleName, propList)
end tell
(*Arguments: reference to document, name of style, and property record*)
on getCellStyle(docRef, styleName, propList)
   tell application "Adobe InDesign CC 2019"
	tell docRef
	   if not (exists cell style styleName) then
		set thisStyle to make cell style with properties {name:styleName}
	   else
		set thisStyle to cell style styleName
	   end if
	   set properties of thisStyle to propList
	end tell
   end tell
   return thisStyle
end getCellStyle

After running this script you should see a listing for "HeadCell" in InDesign's cell styles panel.

With this cell style established for the document, the above script Style Cell Text could be written as follows:

Style Cell Text_Cell Style

(*Assumes cell style "HeadCell" and paragraph style "Body" exist for the document. 
Expects a text frame with tab delimited text to be selected.*)
tell application "Adobe InDesign CC 2018"
   set selList to selection
   if (selList is not {}) and (class of item 1 of selList is text frame) then
	set frameRef to item 1 of selection
	set docRef to document 1
	tell docRef
	   set headStyle to cell style "HeadCell"
	   set bodyStyle to paragraph style "Body"
	   tell text 1 of frameRef
		set tableRef to convert to table column separator tab row separator return
	   end tell
	   tell row 1 of tableRef to merge cell 1 with cell -1
	   set applied cell style of cell 1 of tableRef to headStyle
	   tell text of cells 2 through -1 of tableRef
	      set applied paragraph style to bodyStyle
	   end tell
      end tell
   end if
end tell

Notice that in this version of the Style Cell Text script the styling for the text for all cells but the first is applied directly to the text referenced in the cells after the table is created. If styling is applied to the text before being converted to the table, the applied paragraph style will override the paragraph style in the cell style. Interesting! Maybe it’s time we start looking at table style regions and table styles.

Notice: To keep script examples short and to the point very little checking is done to make sure selected items are as expected. Pay attention to the comments at the top of the scripts.)

What About Microsoft Excel

InDesign has good support for tables imported from Microsoft Excel, but not the newest .xlsx format. Even with .xls, for anything other than the most basic styling, you are out of luck there. We will look at some of the issues in our next blog post.

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.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18391&PostID=1527883&A=Trackback
Trackbacks
Post has no trackbacks.