(* tiff and jpeg must be in same location
works on currently open InDesign document *)

tell application "Adobe InDesign CS3"
	set myDocument to document 1
	set thePageCount to count pages of myDocument
	set relinkcount to 0
	repeat with i from 1 to thePageCount
		set theImages to rectangles of page i of myDocument
		repeat with eachRectangle in theImages
			set theGraphic to all graphics of eachRectangle
			repeat with eachImage in theGraphic
				--return properties of eachImage
				set theLink to item link of eachImage
				set thePath to file path of theLink
				set theBase to my getFileBase(thePath)
				--display dialog theBase
				set theContainerlist to items 1 through -2 of my list_proc(thePath, ":", "")
				set AppleScript's text item delimiters to ":"
				set thecontainer to (the theContainerlist as string) & ":" as string
				set AppleScript's text item delimiters to ""
				if thePath ends with "jpg" or thePath ends with "jpeg" then
					set theNewPath to theBase & ".tif"
					relink theLink to theNewPath
					set relinkcount to relinkcount + 1
				end if
			end repeat
			--return properties of theGraphic
			--return properties of eachRectangle
			--set Hscale to horizontal scale of eachRectangle
			--display dialog Hscale
		end repeat
		
	end repeat
	
	display dialog "Relinked " & relinkcount & " JPEGS to TIFFs."
	
end tell

on getFileBase(fFile)
	
	try
		set fFileTest to text -5 through -1 of fFile
	on error
		set fFileTest to ""
	end try
	if fFileTest contains "." then
		set dotOffsetTest to offset of "." in fFileTest
		set fFileJPEG to text 1 through (-7 + dotOffsetTest) of fFile
	else
		set fFileJPEG to fFile
	end if
	--set fFileJPEG to fFileJPEG & ".jpg"
	return fFileJPEG
end getFileBase

on list_proc(searchList, search_string, replace_string)
	set AppleScript's text item delimiters to the search_string
	set searchListlist to every text item of searchList
	set AppleScript's text item delimiters to replace_string
	set searchList to the searchListlist as string
	set AppleScript's text item delimiters to ""
	
	if replace_string = "" then
		return searchListlist
	else
		return searchList
	end if
	
end list_proc
-- applescript page