Automating Adobe InDesign 2017


Placing Multiple Images Multiple Times

Sunday, December 17, 2017

Multiple Images

Often are the times you may need to place multiple images in a document. If placing manually you would probably use the place gun. But if an image, or multiple images need to be placed multiple times, you might want a better (more automated) way to do it.

Placing Multiple Images

Perhaps you have a catalog where a specific image is used to designate each item category. The containers for the images could be flagged to indicate the category. All that would be needed is a script to place the graphic required for the category. If, for instance, the category were "personal" the "personal.jpeg" image would be placed. Another possibility would use a number for the tag to designate the item within a list. The latter option would provide more flexibility as items within the list could be changed. The following scripts demonstrate:

Catalog

This script places the images to rectangles which have been tagged with strings representing the index of the name of the image to be placed from within a list as in "1", "2", and so on. It assumes the images are saved on the user's desktop in a folder indicated by the variable thePath.

set theFolder to (path to desktop from user domain) as string
set thePath to "catalog:images:"
set pics to ["garden.jpeg", "family.jpeg", "personal.jpeg", "health.jpeg", "books.jpeg"]
set theCount to 0
tell application "Adobe InDesign CC 2017"
   set measurement unit of script preferences to points
   set docRef to active document
   set containers to rectangles of docRef
   try
      repeat with i from 1 to length of containers
	set theRect to item i of containers
	set theTag to name of item i of containers
	set theIndex to theTag as integer
	set imageName to item theIndex of pics
	set imagePath to (theFolder & thePath & imageName) as alias
	tell theRect
	   place imagePath
	end tell
	set theCount to theCount + 1
      end repeat
   end try
end tell
activate
display dialog "Images placed: " & theCount giving up after 5

List Items

In this example, the images are placed at the beginning of paragraphs in the order in which they are indexed within the "pics" list. The container of the images is styled using an object style defined by the variable objStyleName. Text frames containing the targeted paragraphs have been named as identified by the variable frameName.

set theFolder to (path to desktop from user domain) as string
set thePath to "catalog:images:"
set pics to ["garden.jpeg", "family.jpeg", "personal.jpeg", "health.jpeg", "books.jpeg"]
set objStyleName to "Images"
set frameName to "listItems"

tell application "Adobe InDesign CC 2017"
   set measurement unit of script preferences to points
   set docRef to active document
   set spreadRef to spread 1 of docRef
   set objStyleRef to object style objStyleName of docRef
   set frameList to text frames of docRef where name is frameName
   repeat with i from 1 to length of frameList
      set frameRef to item i of frameList
      set paraArr to paragraphs of frameRef
	repeat with j from 1 to length of paraArr
	   set pictName to item j of pics
	   set pictRef to (theFolder & thePath & pictName) as alias
	   set paraRef to (a reference to paragraph j of frameRef)
	   tell insertion point 1 of paraRef
	      place pictRef
	   end tell
	   set theRect to rectangle 1 of paraRef
	   set geometric bounds of theRect to {0, 0, 12, 12}
	   set applied object style of theRect to objStyleRef
	   tell theRect to fit given center content
	end repeat
   end repeat
end tell
frameList

End of Story

Instead of placing multiple items, you may want to place the same image multiple times. This could be a document with a nunber of stories at the end of which the client wants a specific image to be placed. Again a simple script can be written to do the work. This script assumes that each story is either contained by a single text frame or threaded through linked text frames.

Images to be placed should be exact size so as to fit within the line spacing (leading) of the paragraph.

(*Places the image chosen by the user at the end of every story in the active document.
Reports at end of number of stories affected.*)

set imageRef to choose file with prompt "Choose image for placing"
set theCounter to 0
tell application "Adobe InDesign CC 2017"
   set docRef to document 1
   set objStyleRef to object style "Images" of docRef
   set foundStories to stories of docRef
   try
      repeat with i from length of foundStories to 1 by -1
	set storyRef to item i of foundStories
	tell insertion point -1 of storyRef
	   set thePlaced to place imageRef
	end tell
	set theImage to item 1 of thePlaced
	set theContainer to parent of theImage
	tell theContainer
	   set applied object style to objStyleRef
	   fit given center content
     	end tell
        set theCounter to theCounter + 1
      end repeat
   end try
end tell
display dialog "End images placed: " & theCounter giving up after 5

On Your Own

In the End of Story script, If the text frame does not accommodate the image, an overset condition will occur. You might want to add a procedure to check for the overflows state of the text frame container and adjust its size accordingly.

Onward and Upward

As you can see these are simple scripts that demonstrate a specific functionality. They could be part of a larger script that performs a number of tasks to automate a document. You may think of some similar applications that could take advantage of these concepts.

Disclaimer

Scripts are provided as examples for educational purposes only. No representation is given to the correctness nor completeness of the scripts. Users are advised to use these scripts at their own discretion.

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

Recent Posts


Tags


Archive