Your prescription for increased productivity and profitability
Recently I was given a project that involved creating a table–a big table that would involve many pages. The text for the table had been prepared, I was told, using tab/return delimited text.
Not everything that I do in InDesign requires using a script, and this project looked like a typical one-off. Without much afterthought, I created the document based on a template that includes a primary text frame.
With the cursor in the primary text frame, I imported the text for the table—manually. Then, selecting all, I used InDesign’s Convert Text to Table from the Table menu.
The result was anything like I anticipated. Instead of having a table with 2 columns, there were 11 columns. The first two had text and the rest were blank.
I was not about to go into this huge text file to see where the problem was. So, as in so many instances, I launched Script Editor and decided it was time to put some power behind fixing this table.
The first task was to get rid of all of those empty columns. I quickly created the following “fixit” script. It depends on the cursor being placed in one of the table cells.
tell application "Adobe InDesign CC 2015" set measurement unit of script preferences to points --define insertion point selection set selList to selection set selItem to item 1 of selList if class of selItem is not insertion point then return end if set docRef to active document --set table width to page width minus margins set pageWid to my getPageWidth(docRef) set wid to page width of document preferences of docRef set tableWid to (wid - 72) --merge empty cells with column 2 cells set tableRef to parent of parent of selItem set width of tableRef to tableWid set numRows to count of rows of tableRef repeat with i from 3 to numRows tell row i of tableRef merge (cells 2 through 11) end tell end repeat set width of column 1 of tableRef to tableWid / 2 end tell
The next step was to style the table. This, I had intended to do with a script that I often rely on. For me, styling a table takes a little more work than I want to do unnecessarily. Consequently, every time I style a table, I make sure to save the style in a styleSheet. Styles in a stylesheet are easily reused with the following script. The particular style sheet I chose has a wealth of table styles to choose from:
tell application "Adobe InDesign CC 2015" try set tableRef to my getTableFromSelection() set appPath to file path as string set styleFolder to appPath & "Presets:Styles" set styleList to list folder styleFolder without invisibles set userChoice to my getUserChoice(styleList, "select file for styles", false) set styleFile to (appPath & "Presets:Styles:" & userChoice) as alias tell document 1 import styles format table styles format from styleFile set styleList to name of table styles set tableStyle to my getUserChoice(styleList, "select table style to use", false) set styleRef to table style tableStyle end tell set applied table style of tableRef to styleRef on error errStr activate display alert "Error: " & errStr end try end tell (*Returns user choice from choice list*) on getUserChoice(choiceList, promptStr, allowMultiple) set userChoice to choose from list choiceList with prompt promptStr multiple selections allowed allowMultiple if userChoice is not false then if allowMultiple is false then return item 1 of userChoice else return userChoice end if else error "User Cancelled" end if end getUserChoice (*Returns reference to table if cursor is in cell of table*) on getTableFromSelection() tell application "Adobe InDesign CC 2015" set selList to selection if class of item 1 of selList is not insertion point then error "Requires insertion point inside of table cell" end if if class of parent of parent of item 1 of selList is not table then error "Requires insertion point inside of table cell" end if set tableRef to parent of parent of item 1 of selList end tell end getTableFromSelection
Even if your style sheet doesn’t have a style that exactly matches your present need, you might have one that is close enough to set up most of the needed style criteria. You may need to change font size or styling, but most of the work should be done for you.
For creating a table style I rely heavily on cell styles set up for table regions. Styling for the text in the cell styles relies on paragraph styles. And, if possible, I start with a style from a style sheet.
The following sample script allows choosing the style sheet and then opens the style sheet (assuming I will be saving the style I will be creating there). With this, the script lists the styles. When the stye I choose is selected, the script creates a table styled with the style that lists property names and values set for the style.
tell application "Adobe InDesign CC 2015" set measurement unit of script preferences to points set appPath to file path as string set stylePath to appPath & "Presets:Styles" set fileList to list folder stylePath without invisibles set fileChoice to my getUserChoice(fileList, "Choose file for table styles", false) set docRef to open file (stylePath & ":" & fileChoice) set docRef to active document set pageRef to active page of active window tell docRef set styleList to name of table styles set baseStyle to my getUserChoice(styleList, "choose style for base", false) set tRef to table style baseStyle end tell tell text frame 1 of pageRef set tableRef to make table with properties {applied table style:baseStyle, header row count:1, body row count:16, column count:2} end tell set textContent to {"Property Name", "Property Value", "Body region cell style ", name of body region cell style of tRef, "Header region cell style", name of header region cell style of tRef, "Footer region cell style", my getTextEq(footer region cell style of tRef), "Left column region cell style", my getTextEq(left column region cell style of tRef), "Right column region cell style", my getTextEq(right column region cell style of tRef), "top border stroke type ", (name of top border stroke type of tRef), "top border stroke weight ", my tableEq(top border stroke weight of tableRef), "left border stroke type", (name of left border stroke type of tRef), "left border stroke weight", my tableEq(left border stroke weight of tableRef), "bottom border stroke type ", (name of bottom border stroke type of tRef), "bottom border stroke weight ", my tableEq(bottom border stroke weight of tableRef), "right border stroke type ", (name of right border stroke type of tRef), "right border stroke weight ", my tableEq(right border stroke weight of tableRef), "Start row fill count", ("" & start row fill count of tRef), "Start Row Fill Color", name of start row fill color of tRef, "End Row Fill Color", name of end row fill color of tRef, "Skip first alternating fill rows", ("" & skip first alternating fill rows of tRef)} set contents of tableRef to textContent tell cells of tableRef to clear cell style overrides end tell (*Returns value of style property accessed from table itself *) on tableEq(styleProp) if styleProp > 0 then return styleProp as string end if end tableEq (*Returns value of style property accessed from table style *) on getTextEq(styleProp) tell application "Adobe InDesign CC 2015" if styleProp is not nothing then return name of styleProp else return "not defined" end if end tell end getTextEq
The script above is just a partial listing, but should give you an idea of how to create a script like this of your own.
Using the list as a guide, I create new cell styles and a table style based on the settings already in place for this style.
For curiosity sake, once my project was satisfactorily completed, I decided to see what the result would have been had I used a script to import the text and convert it to a table. I already had such a script just needed to find it in my unorganized collection of scripts.
Using another document created from the same template I then used the following script to import the text and convert it to a table. Voila! It worked perfectly a table with two columns. So what was the differnce betweeen importing manually and importing using the script? That may be answered in a forthcoming blog because to date I have not pin-pointed the difference.
tell application "Adobe InDesign CC 2015" --identify the table set selList to selection set selItem to item 1 of selList if class of selItem is insertion point then set textRef to my getTextFile() set flowRef to parent of selItem set frameRef to item 1 of text containers of flowRef tell frameRef to place textRef tell text 1 of frameRef set tableRef to convert to table column separator "\t" row separator "\r" end tell end if end tell
I think my next task is to come up with a script that organizes my scripts so I am not tempted to do a task manually that can be better handled with a script.