AUTOMATING IMAGE PLACEMENT

Our previous blog demonstrated how a primary text frame (on the Master-A spread) could be used to import a text file that automatically flows to all pages one paragraph per page. In this post we will finish the project with a script to import the images for the document pages.

WHAT YOU NEED

For the project you will need to have a folder with images, one for each page of your document. We sized the images at 3-3/4 inches wide by 4 inches tall. We will leave the resolution for the images up to you. To accommodate the image size, we changed the margin settings for the document from our previous post slightly. We will be using the document created in the previous post. If you don’t have that document, create a document for testing measuring 5 x 7 inches, non-facing pages, with primary text frame and pages to accommodate the number of images in your folder. Margin settings need to be as set to match the values for a variable (mList) established at the top of the script. These are in the order top: 1 inch, left and right: .62 inches, and bottom: .75 inches.

IMPORTING IMAGES

In our last post, using the primary text frame for placing a text file worked seamlessly. With this in mind, one might think that an image frame on the master page could work similarly. That is possible, but the code is much simpler if the images are placed directly to the page. Once placed, the image container is positioned using geometric bounds. Next, the image is fit using center content. Our demonstration script will assume that facing pages is false so there is no need to make adjustments for inside and outside margins (see previous blog post).

<2>Choosing Image Files<\h2>

There are a number of ways the images for the document can be defined within the script. Here we will demonstrate two.

Choose Folder

The first option for defining images to be placed will simply involve the user choosing the folder containing the images. This works well if there is no criteria for the order in which the images are to be placed.

set promptStr to "Select folder of images"
try
   set {folderPath, fileList} to getFiles(promptStr)
on error errStr
   activate
   display alert "Error " & errStr
end try
(*Returns list of files in folder in no particular order*)
on getFiles(promptStr)
   set folderRef to choose folder with prompt promptStr
   set folderPath to folderRef as string
   set fileList to list folder folderPath without invisibles
   return {folderPath, fileList}
end getFiles 

Choose File to Read Filenames

The second option will have the user select a text file that contains a paragraph for each of the image file names in the order in which they are to be placed. For this the text file will reside in the same folder as the image file folder. The name of the image file folder will be defined at the top of the script.

set readFilePrompt to "Select text file containing image names"
set nameOfImageFolder to "Images"
try
	set {folderPath, fileList} to readFileForNames(readFilePrompt, nameOfImageFolder)
on error errStr
	activate
	display alert "Error " & errStr
end try
(*Returns path to images based on image folder residing in the same folder as the text file chosen*) 
on readFileForNames(readFilePrompt, nameOfImageFolder)
	set fileAlias to choose file with prompt readFilePrompt
	set filePath to fileAlias as string
	set fileNames to read file filePath as list using delimiter {"\n"}
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set pathList to text items of filePath as list
	set folderPath to ((items 1 through -2 of pathList) as string) & ":" & "Images:"
	set AppleScript's text item delimiters to oldDelim
	return {folderPath, fileNames}
end readFileForNames 

Notice that both options for getting the list of file names return the path to the image folder and the list of the image file names. With this, both options can use the same handler to complete the script.

PLACING THE FILES

Given the bounds for the first page of the document, the script calculates the image’s geometric bounds. After this calculation, the script then calls the placeFiles_toMargins handler. This handler places the images, sets the geometric bounds, and applies fitting.

--add to variables at the top of the script
set mList to {1.0, 0.62, 0.75, 0.62}
set frameHgt to 4.0
set promptStr to "Select folder of images"
--For the choose folder option, the top of the script will read
try
	set {folderPath, fileList} to getFiles(promptStr)
        placeFiles_toMargins (mList, frameHgt, folderPath, fileList)
	
on error errStr
	activate
	display alert "Error " & errStr
end try
(*Places images to fit top, left, and right margins. The bottom bounds is calculated using the value of the frameHgt variable.*)
on placeFiles_toMargins (mList, frameHgt, folderPath, fileList)
   tell application "Adobe InDesign CC 2019"
	set measurement unit of script preferences to inches decimal
	copy mList to {tp, lft, bot, rgt}
	tell document 1
	   set masterSpread to master spread 1
	   tell page 1 of masterSpread
		copy bounds to {y0, x0, y1, x1}
		set gBounds to {tp, lft, tp + frameHgt, x1 - rgt}
	   end tell
	   repeat with i from 1 to count of pages
		set imageRef to folderPath & item i of fileList
		tell page i
		   set placeList to place file imageRef
		end tell
		set imageRef to item 1 of placeList
	        tell parent of imageRef
		   set geometric bounds to gBounds
		   fit given center content
		end tell
	   end repeat
	end tell
   end tell
end placeFiles_toMargins 
on getFiles(promptStr)
	set folderRef to choose folder with prompt promptStr
	set folderPath to folderRef as string
	set fileList to list folder folderPath without invisibles
	return fileList
end getFiles

For the Choose File to Read Option the script changes slightly. Instead of the promptStr variable, a variable readFilePrompt will be used. A variable to define the name of the image folder will also be added.

set mList to {1.0, 0.62, 0.75, 0.62}
set frameHgt to 4.0
set readFilePrompt to "Choose text file to read for Image names"
set nameOfImageFolder to "Images"

The main section of the script will also change slightly:

try
	set {folderPath, fileList} to readFileForNames(readFilePrompt, nameOfImageFolder)
	placeFiles_toMargins(mList, frameHgt, folderPath, fileList)
	{folderPath, fileList}
on error errStr
	activate
	display alert "Error " & errStr
end try

Make sure the placeFiles_toMargins and readFileForNames handlers are at the bottom of the script (see above).

FINISHING UP

With either script you should have a document at this point that, with some minor additions, is ready to be exported as a PDF or published to web. One option for PDF would be to enable full screen viewing where the pages change automatically. For this, make sure to select Adobe PDF (interactive) for the Format when you Export (Command + E). Then check Open in Full Screen Mode in the General panel when you click Save.

…Sample settings for a pdf flip book

A small problem with this option is that the person viewing the PDF must allow the script to take over the computer screen.

Of course you can save as a PDF without this setting. The PDF can be viewed using the controls provided by the viewing application.

As an alternative to PDF, you might decide to save the document using Publish Online. Simply choose Publish Online… from InDesign’s File menu.

…Sample Settings for Publish Online

Although both the PDF viewer and Publish to Web have their own methods for navigating, you might decide to add navigation buttons to you pages. We will work with buttons and path points in a later blog post. Meanwhile you might want to create a nice cover for your document and place copyright statements on a back page.

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.