Place image into InDesign if the image name equals the filename

I’m working on an InDesign script that will build a new set of files for a job number based on templates that are pre-built, I was able to get to the hardest point so far, and that is my folder is built containing all of the renamed templates and then each Indesign file is opened and certain text information inside each file is changed to reflect the specific file at hand

I now am trying to make it so the script will gather a list of images in a folder that I choose and if certain criteria in the file name of the image matches certain criteria in the Indesign file name then the image will be placed into the Indesign file.

Example:
the Indesign document is called “M2414_A.indd”, and I have a list of images (M2414A.tif, M2414B.tif, M2414C.tif,)
I need to only place “M2414A.tif” into the “M2414_A.indd” file

I’m racking my brain trying to figure it out, right now it’s beyong the scope of what I know, any help would be appreciated

Also, this script is for Indesign CS 2, which uses a nice script labeling feature, letting you now give names to elements in a document which can be called directly by applescript…very nice feature.

Here’s my full script, the area in question is down by the bottom, where I am actually opening the indd files one at a time and adding the file specific content.


property file_types : {"TIFF"} --adjust as necessary
property file_extensions : {"tif", "tiff"} --adjust as necessary
set originalFolderPath to choose folder with prompt "Please locate the templates for your job"
set dd to display dialog "What is the FCB Job Number" default answer "XXXXX"
set fcbJobNumber to text returned of dd
set destination to (choose folder with prompt "Choose a destination for the job folder.") as string
set the_folder to (choose folder with prompt "Please locate your art for the new job")
set the_images to get_folder_list(the_folder, file_types, file_extensions, true, false)
if the_images = {} then
	beep
	display dialog "No valid images found in chosen folder." buttons {"OK"} default button 1 with icon 0 giving up after 10
	return
end if
--set image_count to (count the_images)

set jobFolderPOSIX to POSIX path of (destination & "ASCA_TRAN_" & fcbJobNumber) & "/"
do shell script ("mkdir -p " & jobFolderPOSIX)
tell application "Finder" to update folder destination

tell application "Finder"
	set templateList to every file in folder originalFolderPath whose name ends with ".indd"
	duplicate (every file in folder originalFolderPath whose name ends with ".indd") to folder ¬
		(destination & "ASCA_TRAN_" & fcbJobNumber) with replacing
	set newFileList to every file in folder (destination & "ASCA_TRAN_" & fcbJobNumber)
	repeat with aFile in newFileList
		set currentFileName to name of aFile
		set the name of aFile to fcbJobNumber & "_" & currentFileName
		set newFileList to every file in folder (destination & "ASCA_TRAN_" & fcbJobNumber)
	end repeat
end tell

tell application "Adobe InDesign CS2"
	--activate
	repeat with aFile in newFileList
		open aFile
		set fileName to name of aFile as string
		--try
		select text frame "adIdNumber" of text frame "jobBox" of page 1 of document 1
		set contents of selection to "ASCA_TRAN_" & fcbJobNumber & "_" & character 7 of fileName
		select text frame "smallJobNumber" of text frame "jobBox" of page 1 of document 1
		set contents of selection to "ASCA_TRAN_" & fcbJobNumber
		select text frame "smallFileName" of text frame "jobBox" of page 1 of document 1
		set contents of selection to name of aFile as string
		select text frame "buildDate" of text frame "jobBox" of page 1 of document 1
		set contents of selection to "1"
		select text frame "largeJobNumber" of page 1 of document 1
		set contents of selection to characters 1 thru 5 of fileName as string
		select text frame "componentCode" of page 1 of document 1
		set contents of selection to character 7 of fileName as string
		repeat with thisImage from 1 to the_images
			set imageName to name of the thisImage as string
			set imageToPlace to (thisImage whose (character 6 of imageName as string) = (character 6 of fileName as string))
		end repeat
		select rectangle "imageBox" of page 1 of document 1
		place thisImage on selection
		--end try
		--close document aFile saving yes
	end repeat
end tell

on get_folder_list(the_folder, file_types, file_extensions, with_subfolders, inc_folders)
	set the_files to {}
	tell application "Finder" to set folder_list to every item of folder the_folder
	repeat with new_file in folder_list
		try
			set temp_file_type to file type of new_file
			set temp_file_extension to name extension of new_file
		on error
			set temp_file_type to "folder"
			set temp_file_extension to ""
		end try
		if file_types contains temp_file_type or file_extensions contains temp_file_extension then set end of the_files to (new_file as string)
	end repeat
	return the_files
end get_folder_list

If the job file name is “M2414_A” and the art file name is “M2414A” (not counting the name extension) then comparing letters 6 won’t work. You can eliminate the underscore with a simple search and replace, then get rid of the file extensions and compare the names directly rather than by a specific letter, or just compare letter 6 of the art file name to letter 7 of the job file name.

Also, this…

repeat with thisImage from 1 to the_images
           set imageName to name of the thisImage as string
           set imageToPlace to (thisImage whose (character 6 of imageName as string) = (character 6 of fileName as string))
       end repeat
       select rectangle "imageBox" of page 1 of document 1
       place thisImage on selection

… isn’t doing what you intend. You’re placing thisImage which is the last iteration of the loop, not the imageToPlace. And the test should be:

if (character 6 of imageName) = (character 7 of fileName) then set imageToPlace to thisImage

And the repeat block begins incorrectly. It should be:

repeat with thisImage from 1 to count of the_images

You don’t necessarily have to select the object to place art on it.

place imageToPlace on rectangle "imageBox" of page 1 of document 1

should work just as well.

ID can’t get the name of a file reference string so ‘set imageName to name of thisImage as string’ will error. You will either need to call the Finder to get the name or parse it with your own method.