(* Script runs on current open document in InDesign CS2 or CS3
Checks each non-EPS image, gets image scale, opens in PhotoShop CS2 or CS3, scales image to 100% (or as close to it as possible) at resolution set in variable theResolution (with resizing)
if changecolormode is set to true, changes color mode to whatever is set in variable theColorMode (RGB, CMYK or grayscale)

saves image as TIFF (also saves JPEG if original was JPEG)
  if images were jpegs, run the "indesign linked images jpeg to tiff" script to switch to the TIFF versions
*)

--set desired resolution here
set theResolution to 150
--set changeColorMode to false
set changeColorMode to true
--set desired color mode here if changeColorMode is true
--set thecolormode to "RGB"
set theColorMode to "CMYK"
--set theColorMode to "grayscale"

tell application "Adobe InDesign CS3"
	set myDocument to document 1
	set thePageCount to count pages of myDocument
	
	repeat with i from 1 to thePageCount
		set theImages to rectangles of page i of myDocument
		repeat with eachRectangle in theImages
			set HscaleRectangle to horizontal scale of eachRectangle
			if HscaleRectangle ≠ 100 then
				--set horizontal scale of eachRectangle to 100
				redefine scaling eachRectangle to {1.0, 1.0}
				--display dialog "this script may be inadequate; rectangle is scaled, gotta deal with that?"
			end if
			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 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 ""
				--return properties of theLink
				--display dialog thePath
				set Hscale to absolute horizontal scale of eachImage
				set Vscale to absolute vertical scale of eachImage
				--display dialog Hscale
				
				if thePath does not end with "eps" then
					tell application "Adobe Photoshop CS3"
						open file (thePath as string) showing dialogs never
						set docRef to current document
						set theName to name of docRef
						resize image docRef width Hscale as percent height Vscale as percent resolution theResolution
						if changeColorMode then
							if theColorMode = "grayscale" then
								set theColorModePhotoShop to grayscale
							else if theColorMode = "RGB" then
								set theColorModePhotoShop to RGB
							else if theColorMode = "CMYK" then
								set theColorModePhotoShop to CMYK
							end if
							if (mode of docRef is not theColorModePhotoShop) then
								change mode docRef to theColorModePhotoShop
								say "changed one to " & theColorMode as string
							end if
						end if
						if theName does not end with "tif" and theName does not end with "tiff" then
							set theNewName to my getFileBase(theName) & ".tif"
							set fileSpec to thecontainer & theNewName as string
							save docRef in file fileSpec as TIFF with copying
						end if
						close current document with saving
					end tell
				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 thePageCount
	
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