Your prescription for increased productivity and profitability
It’s time to move on from tables filled with just boring text. Adding images to a table can make it more interesting and even help clarify a concept. But working with images in tables can be anything but easy—that is until the 2015 version of InDesign. Prior to that version the only way images could be placed was as inline objects. That is because a table cell is actually a text frame. Check it out. Select a text frame that contains a table and run the following:
tell application "Adobe InDesign CC 2019" set tableRef to table 1 of item 1 of selection get cell type of cell 1 of tableRef end tell
If you had a text frame with a table selected when you ran the script above, you should see the result:
text type cell
With a text type cell, you can place an image similar to placing an image to any other text frame, using an insertion point or other text reference.
set fileChoice to choose file with prompt "Select image to place" tell application "Adobe InDesign CC 2019" set tableRef to table 1 of item 1 of selection set cellRef to cell 1 of tableRef tell insertion point 1 of cellRef place fileChoice end tell end tell
The problem is you now have an inline image in the cell which is a little problematic when it comes to manipulating the image (resizing, moving, etc.).
Now that we have the cell type property of a cell, working with images is much easier. To convert a text type cell into a graphic type cell you can’t just set the property. You need to convert the cell. And here is the big statement. Print it out in 72 pt type and paste it somewhere handy.
tell cellRef convert cell type final cell type graphic type cell end tell
Using this handy little statement you now have a cell with a rectangle for placing the image. Again, select a text frame with a table and run the following:
tell application "Adobe InDesign CC 2019" set tableRef to table 1 of item 1 of selection set cellRef to cell -5 of tableRef tell cellRef convert cell type final cell type graphic type cell end tell get cell type of cellRef end tell
When you run the script, the result should now be:
graphic type cell.
Your script can now use the rectangle inside the cell to place an image:
set imageRef to choose file with prompt "Select image file to place" tell application "Adobe InDesign CC 2019" set tableRef to table 1 of item 1 of selection set cellRef to cell 1 of tableRef tell cellRef convert cell type final cell type graphic type cell end tell tell rectangle 1 of cellRef place imageRef end tell get cell type of cellRef end tell
Your script can now use fit given frame to content:
tell rectangle 1 of cellRef place imageRef fit given frame to content end tell
By sizing the height of the cell first, you can use a variety of fitting options:
set height of cellRef to “144 pt” tell rectangle 1 of cellRef place imageRef fit given proportionally end tell
A script can also use properties of the cell to fit the image:
tell item i of cellLiist set fileName to (text 1 of it) convert cell type final cell type graphic type cell set minimum height to "144 pts" set maximum height to "144 pts" set clip content to graphic cell to true end tell
One automated solution is to use tab-delimited text to create the table. The text for cells designated to place images can be the file name for the cell.
In our example, the tab delimited text for the table is defined in the variable tabDelim
set tabDelim to "Blackberry.jpg" & tab & "Strawberry.jpg" & tab & "Blueberry.jpg" & tab & & "Raspberry.jpg"
Of course, the file name will need to be added to the path to the folder containing the files. All of our files for the sample table are in a folder named “Images” on the Desktop. They are all .jpeg files measuring 144×144 pts. To define the path to the folder, the script uses path to:
set folderPath to path to desktop from user domain as string set folderStr to folderPath & "Images:"
We have the script create the text frame using point measurements. This is controlled by setting the measurement unit for script preferences to points.
set myBounds to {30, 15, 180, 591} (*within a tell statement block to InDesign define the tabDelim string and add:*) set measurement unit of script preferences to points tell page 1 of document 1 set frameRef to make text frame with properties {geometric bounds: myBounds, contents:tabDelim} end tell
The table is created using convert to table
tell text 1 of frameRef set tableRef to convert to table column separator tab row separator return end tell
Since we have the script define the size of the images (imageSz), it can set the height of the row using
tell row 1 of tableRef set maximum height to imageSz set minimum height to imageSz end tell
Because all of the files have a filename extension of .jpg, the script can get a list of the cells for placing the images using:
tell tableRef set cellList to every cell of tableRef where contents of it ends with ".jpg" end tell
Then within a repeat loop, the text contents of the cell is added to the folderStr variable and the magic happens. The complete script is as follows:
set imageSz to 144 set numImages to 4 set myBounds to {30, 15, 180, 591} set tabDelim to "Blackberry.jpg" & tab & "Strawberry.jpg" & tab & "Blueberry.jpg" & tab & "Raspberry.jpg" set folderPath to path to desktop from user domain as string set folderStr to folderPath & "Images:" tell application "Adobe InDesign CC 2019" set measurement unit of script preferences to points tell page 1 of document 1 set frameRef to make text frame with properties {geometric bounds: myBounds, contents:tabDelim} end tell tell text 1 of frameRef set tableRef to convert to table column separator tab row separator return end tell tell row 1 of tableRef set maximum height to imageSz set minimum height to imageSz end tell set width of tableRef to imageSz * numImages tell tableRef set cellList to every cell of tableRef where contents of it ends with ".jpg" end tell repeat with i from 1 to length of cellList set cellRef to item i of cellList tell cellRef set fileName to (text 1 of it) convert cell type final cell type graphic type cell set clip content to graphic cell to true end tell set filePath to folderStr & fileName tell rectangle 1 of cellRef place file filePath end tell end repeat end tell
Notice how the name of the file is added to the path to the folder containing the files. It’s pretty slick. Just think of a series of tables all having a row of images to highlight the columns.
To run the script above you will need an InDesign document open and some “.jpg” images in an “Image” folder on the desktop. Define the number of images (numImages) and their width (imageSz) at the top of the script. Make sure your string that defines the image names (tabDelim) is accurate (change the names in our example to match that of your files).
In working with tables make sure you do not set measurements to exceed the table dimensions. If the table oversets for any reason (including the width of rules), your scripts will fail with errors that are not intuitive. So if you do get a failure, double check your measurements first.
Our last post of this series will pick up some of the miscellaneous issues that are left having to do with automating tables in InDesign. Then, just wait for our blog series that deals with XML; automating tables using XML is a whole new world.
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.