Rename/relink in InDesign without text file

Thanks to the wonderful people on this site I have the following script. I would like to take it one step further: Instead of relying on a text file to rename the links, I want the script to prompt the user for the beginning of the file name and append a consecutive number.
I.e. Workbook_00001 or StudentEdition_00001

set the_list to (read (choose file with prompt "Choose the file that contains the new file names:"))'s paragraphs
set source_folder to (choose folder with prompt "Choose the folder with the graphic files to rename:") as text
set {TID, text item delimiters} to {text item delimiters, tab}
tell application "Finder"
	repeat with this_para in the_list
		--set {source_file, new_name} to my string_to_list(this_para, ",")
		set {source_file, new_name} to my string_to_list(this_para, tab)
		try
			set name of ((source_folder & source_file) as alias) to new_name
		on error e
			activate
			display dialog e buttons {"Cancel", "OK"} default button 2 with icon 0 giving up after 5
		end try
	end repeat
end tell

on string_to_list(s, d)
	tell (a reference to my text item delimiters)
		set {o, contents} to {contents, d}
		set {s, contents} to {s's text items, o}
	end tell
	return s
end string_to_list

tell application "Adobe InDesign CS4"
	activate
	repeat with this_para in the_list
		set {source_file, new_name} to text items of this_para
		try
			set currentLink to (every link of active document whose file path ends with source_file)
			relink currentLink to source_folder & new_name
			update currentLink
		on error e
			display dialog e buttons {"Cancel", "OK"} default button 2 with icon 0 giving up after 5
		end try
	end repeat
	set AppleScript's text item delimiters to TID
	display dialog "Finished."
end tell

Here’s my situation: I have an InDesign file that has about 600-800 links. Some of these links appear multiple times in the document and some are on master pages. I need to re-name all of the links with a new base name_ consecutive numbers (as talked about above) and then re-link them in InDesign. I don’t even know if this is possible, but there are so many things that are foreign to me in Applescript that I thought it would be worth a shot to ask. Thank you so much!

Hi,

try this


property leadingZeros : 4

set lead to ""
repeat leadingZeros times
	set lead to lead & "0"
end repeat

set {text returned:baseName} to display dialog "Enter a base name" default answer ""
set idx to 1
tell application "Adobe InDesign CS4"
	activate
	set allLinks to links of active document
	repeat with oneLink in allLinks
		set {file path:filePath} to oneLink
		set newName to baseName & "_" & text (-leadingZeros - 1) thru -1 of (lead & idx as text)
		set newPath to my renameFile(filePath, newName)
		relink (contents of oneLink) to newPath
		update oneLink
		set idx to idx + 1
	end repeat
	display dialog "Finished."
end tell

on renameFile(thePath, theName)
	tell application "Finder"
		set {name extension:Ex, container:parentFolder} to file thePath
		set newName to theName & "." & Ex
		set name of file thePath to newName
	end tell
	return ((parentFolder as text) & newName)
end renameFile

Thanks Stefan,

When I ran this I got this error:

“Adobe InDesign CS4 got an error: contents of item 1 of {link id 478 of EPS id 459 of rectangle id 458 of page id 457 of spread id 333 of document “TestDoc.indd”, link id 480 of EPS id 446 of rectangle id 404 of page id 329 of spread id 307 of document “TestDoc.indd”, link id 484 of EPS id 442 of rectangle id 414 of page id 330 of spread id 307 of document “TestDoc.indd”, link id 485 of EPS id 429 of rectangle id 434 of page id 338 of spread id 333 of document “TestDoc.indd”, link id 489 of EPS id 421 of rectangle id 426 of page id 332 of spread id 308 of document “TestDoc.indd”, link id 491 of EPS id 415 of rectangle id 420 of page id 331 of spread id 308 of document “TestDoc.indd”, link id 494 of EPS id 393 of rectangle id 398 of page id 328 of spread id 306 of document “TestDoc.indd”, link id 497 of EPS id 387 of rectangle id 392 of page id 327 of spread id 306 of document “TestDoc.indd”, link id 500 of EPS id 378 of rectangle id 383 of page id 326 of spread id 305 of document “TestDoc.indd”, link id 503 of EPS id 372 of rectangle id 377 of page id 325 of spread id 305 of document “TestDoc.indd”, link id 505 of EPS id 299 of rectangle id 270 of page id 180 of spread id 175 of document “TestDoc.indd”} doesn’t understand the relink message.”

However it renamed the duplicate links that I had but did not re-link them (it showed it was missing in the links palette.) All the other links were not re-named.

hm, I tested the script successfully with Indesign CS3

When I run this in CS3 it re-links everything except for the duplicate files. The error states that it ˜cannot get file’.

When I ran it before (and came up with the error posted above) it was in CS4. This is the version I need to run it in.

After reading through some posts on here I found someone with a similar issue. A member mentioned that re-linking was changed in CS4. Based on a suggestion from that thread I changed:


relink (contents of oneLink) to newPath 

To


relink (contents of oneLink) to file (newPath as string)

This renamed and re-linked 3 of my 11 images. I feel like I’m getting so close! Any suggestions on what steps to take next?

Ok, I am posting the script that I got to work. It will re-name and re-link files from a folder in InDesign CS4. (Will rename all of your links in that folder to your base name plus a consecutive number.) If there are errors it will produce a text file listing the errors.


property leadingZeros : 4

set lead to ""
repeat leadingZeros times
	set lead to lead & "0"
end repeat

set idx to 1
set errors_found to false

set errorsfolder to (choose folder with prompt "Choose the folder in which you would like to save the error messages.") as text
set errorfilename to errorsfolder & "script_errors.txt" as text

set errorfile to open for access errorfilename with write permission
set date_ to (current date) as string
write (return & "Staring new run " & date_ & return) to errorfile starting at eof
close access errorfile

tell application "Finder"
	
	set {text returned:baseName} to display dialog "Enter a base name" default answer ""
	set myfolder to (choose folder with prompt "Choose the folder with the graphic files to rename:") as text
	set file_list to every file of folder myfolder
	repeat with myfile in file_list
		set oldfile to name of myfile
		if oldfile ends with ".eps" then
			set newName to baseName & "_" & text (-leadingZeros - 1) thru -1 of (lead & idx as text) & ".eps"
			
			tell application "Adobe InDesign CS4"
				set mylinks to (every link of active document whose file path ends with oldfile)
				if mylinks is equal to {} then
					set errorfile to open for access errorfilename with write permission
					write "File was not found in document:" & oldfile & return to errorfile starting at eof
					close access errorfile
					set errors_found to true
				else
					set name of myfile to newName -- will throw an error if the short filename already exists
					repeat with currentLink in mylinks
						set fullname to (myfolder & newName) as string
						try
							relink currentLink to file (fullname as string)
						on error e
							set errorfile to open for access errorfilename with write permission
							write "Error re linking from " & oldfile & " to " & fullname & return to errorfile starting at eof
							close access errorfile
							display dialog e buttons {"Cancel", "OK"} default button 2 with icon 0 giving up after 5
						end try
						--update currentLink
					end repeat
					set idx to idx + 1
				end if
				
			end tell
			
		end if
	end repeat
end tell

if errors_found is equal to true then
	display dialog "You're done!: Sorry though, there were some errors. There is a list of them here: " & errorfilename & return
else
	display dialog "Good job! You are done and there are no errors!" & return
end if