[InDesign] Updating Links

I have an applescript I wrote that is used to duplicate InDesign documents. Each document is in a separate folder with all graphic elements within that folder as well. The renames the folder, the INDD and opens the INDD and updates the runcode in the document. What is not doing is updating the links from the old folder to the new folder. It is important to note that the links of the document are still valid, but they need to link to the containing folder of the document not where it came from before.

The following snippet is the focus of my problem:


--newfolderalias is set earlier and IS the alias to the folder containing the active document

			set usedGraphics to links of active document
			repeat with i from 1 to count items in usedGraphics
				set currentGraphic to item i of usedGraphics
				set currentGraphicName to name of currentGraphic as text
				set newpath to newfolderalias & currentGraphicName -- a string file name
				try
					relink currentGraphic to newpath
				end try
			end repeat


In order to put the whole thing in context and because I have not had the time to clean the entire thing up properly. I am going to include the whole thing here it is in its entirety in case some ambitious sole feels like cleaning up the code… (not the real hope of this posting, but it wouldn’t hurt)

display dialog "Drag and Drop an Ad folder here to pick it up"
return -- not needed, but shows that the script stops here when "run"

---Someday I will move this to XCode to limit the dialog boxes...someday
on open of finderObjects -- "open" handler triggered by drag'n'drop launches
	set adnumber to text returned of (display dialog "Enter the new ad number." with icon stop default answer "" buttons {"Continue"} default button 1)
	
	set rundate to text returned of (display dialog "Enter the run date. ex: 7-20" with icon stop default answer "" buttons {"Continue"} default button 1)
	

	set AppleScript's text item delimiters to {"-"}
	set runmonth to text item 1 of rundate
	set runday to text item 2 of rundate
	------------------------
	-- Lets grab todays date & set up runcodes for the filenames
	------------------------
	set thisday to day of (current date) as string
	set thismonth to (month of (current date) as integer) as string
	set thisyear to year of (current date) as string
	
	if (runmonth as integer) < (thismonth as integer) then
		set runyear to thisyear + 1
	else
		set runyear to thisyear
	end if
	
	set runyear to last character of (runyear as string)
	
	if runmonth is "10" then
		
		set runcode to runyear & "A"
	else if runmonth is "11" then
		set runcode to runyear & "B"
	else if runmonth is "12" then
		set runcode to runyear & "C"
	else
		set runcode to runyear & runmonth
	end if
	-----------------------------------
	
	repeat with i in (finderObjects) -- in case multiple objects dropped on applet
		---displayName(i) -- show file/folder's info
		if folder of (info for i) is true then -- process folder's contents too
			set myfolder to i
			
			--for search and replace we need to have the old ad number
			tell application "Finder"
				set oldname to name of myfolder as text
			end tell
			
			set AppleScript's text item delimiters to {" "}
			set oldadnumber to text item 1 of oldname

			set newadnumber to runcode & adnumber
			
			set currentlocation to GetParentPath(myfolder)
			set newfoldername to runcode & adnumber & " " & rundate
			set newfolder to currentlocation & newfoldername
			tell application "Finder" to make new folder at alias currentlocation with properties {name:newfoldername}
			set newfolderalias to newfolder as alias
			---We now have a new folder
			
			tell application "Finder"
				set temp to (entire contents of i)
				
				repeat with j in (temp)
					set theFileCopy to duplicate j to newfolderalias
					
					if name extension of j as string is "indd" then
						set myoldname to name of theFileCopy as text
						set thesize to text item 3 of myoldname
						set newadname to runcode & adnumber & " " & rundate & " " & thesize & " jd.indd"
						set name of theFileCopy to (newadname as Unicode text)
					end if
					---why do we have Creator files?
					if name extension of j as string is "crtr" then
						set myoldname to name of theFileCopy as text
						set thesize to text item 3 of myoldname
						set newadname to runcode & adnumber & " " & rundate & " " & thesize & " jd.crtr"
						set name of theFileCopy to (newadname as Unicode text)
					end if
				end repeat
			end tell
		else
			display dialog "Only works on folders"
		end if
	end repeat
	
--now let's open the INDD and try to do stuff... If this is an old creator file, let the poor sucker, er, user sort it all out....	
	try
		set theFile to (newfolderalias as text) & newadname
		set theFile to theFile as alias
		---display dialog theFile
		tell application "Adobe InDesign CS4"
			set user interaction level of script preferences to never interact
			activate
			open theFile
			
			----Update Links
			set usedGraphics to links of active document
			repeat with i from 1 to count items in usedGraphics
				set currentGraphic to item i of usedGraphics
				set currentGraphicName to name of currentGraphic as text
				set newpath to newfolderalias & currentGraphicName -- a string file name
				try
					relink currentGraphic to newpath
				end try
			end repeat
			
			--Clear the find/change preferences.
			set find text preferences to nothing
			set change text preferences to nothing
			--Search the document for the string "copy".
			set find what of find text preferences to oldadnumber
			set change to of change text preferences to newadnumber
			--Set the find options.
			set case sensitive of find change text options to false
			set include footnotes of find change text options to false
			set include hidden layers of find change text options to false
			set include locked layers for find of find change text options to false
			set include locked stories for find of find change text options to false
			set include master pages of find change text options to false
			set whole word of find change text options to false
			tell active document
				set myFoundItems to change text
			end tell

		end tell
	end try
end open



on GetParentPath(theFile)
	tell application "Finder" to return container of theFile as text
end GetParentPath



An interesting thing happened when I ran the code above on a folder that contained sub-folders. The contents of the sub-folders were copied to the new folder within the sub-folders, but were also duplicated in the root of the new folder. Didn’t expect that one, but I forgot to account for sub-folders at all.