Rename and relink script for InDesign?

I have an InDesign CS3 file that contains approximately 600 links. (All have the same extension: .eps) I need to rename all of the links and then relink the renamed files to the InDesign file. We are renaming our entire catalog of links so this script needs to be very generic so it can be used on all of our documents. I have a script that will successfully rename all of the links using a txt file. I am having issues with getting the renamed files to relink to the InDesign doc. Please help! Here is what I have so far.

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 Unicode text
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 & “.eps”
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 CS3”
set blah to (active document) as alias
set usedGraphics to links of front document as list
repeat with i from 1 to count items in usedGraphics
set currentGraphic to item i of usedGraphics
set newpath to (source_folder as Unicode text) & usedGraphics
try
relink blah to newpath
end try
end repeat
end tell

display dialog " Finished."

When I run this the Script Editor says that it runs fine, but it does not relink the links. Therefore I tried a second ending that looked like this:

tell application “Adobe InDesign CS3”
set source_folder to alias
set MyBigDocument to (choose file)
set source_folder to links of front document as list
repeat with i from 1 to count items in source_folder
relink source_folder to MyBigDocument
end repeat
end tell

display dialog “Finished.”

This one did not give me an error either, but linked the InDesign doc to itself. Thank you in advance for your help. I am new at this, so any input or suggestions would be appreciated!

This can’t work at all

try this, you have to replace “???” with the appropriate file name, probably taken form the_list


tell application "Adobe InDesign CS3"
	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 newpath to source_folder & "???" -- a string file name
		try
			relink currentGraphic to newpath
		end try
	end repeat
end tell

Thanks Stefan. I fumbled around based on your reply and it’s obvious that I should of made it clearer that I’m a TOTAL NEWBIE and any of this code that does happen to work does so because someone else wrote it and I happened to
piece it together correctly.

So could you help me out and tell me exactly what I need to put in for the ‘-- a string file name’

My original text file is named Workbook3.txt. It is a tab delimited file containing the original/old link names in the first column and the new link
names in the second column.

Thank you so much for your patience!

Assuming the text file contains only names including extension and no paths e.g.

file1.eps\tfile11.eps file2.eps\tfile12.eps file3.eps\tfile13.eps
(\t = tab) and all files are in the same folder, try this


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 "Adobe InDesign CS3"
    activate
    repeat with this_para in the_list
        set {source_file, new_name} to text items of this_para
        try
            set currentLink to (1st 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

Note: the links will also be visibly updated

Thank you so much! For those who need it, this is what I ended up with:

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 CS3"
	activate
	repeat with this_para in the_list
		set {source_file, new_name} to text items of this_para
		try
			set currentLink to (1st 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

Just going out on a limb here . since we are hopefully going to be using this for our entire catalog of files it would be good if we could keep it as simple as possible. Is it possible to rename and relink without using a text file? If not or if it’s too complicated that’s not a problem, but you never know unless you ask. :o) Thanks again!

You can gather the data form many sources

It’s a waste of time to go twice to the entire list and first rename and then relink the files.
You can do it in one loop


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 "Adobe InDesign CS3"
	activate
	repeat with this_para in the_list
		set {source_file, new_name} to text items of this_para
		try
			set currentLink to (1st link of active document whose file path ends with source_file)
			my renameFile(currentLink, source_file, new_name)
			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

on renameFile(filePOSIXPath, oldName, newName)
	set filePath to POSIX file filePOSIXPath as text
	tell application "Finder" to set name of file filePath to newName
end renameFile


When I run the above script I get the follow error message:

“Can’t make POSIX file {«class clk» id 376 of PostScript picture id 372 of «class crec» id 377 of «class page» id 325 of «class sprd» id 305 of document “TestDoc.indd” of application “Adobe InDesign CS3”} into type Unicode text.”

This error message appears for every instance of a link with only the numbers changing.

Again, I am such a newbie at this. I know that POSIX is a file specification, but I’m not sure what this error means or how to fix it. Please help. :slight_smile:

Couple of problems:

-Your variable currentLink is not a file path but an InDesign reference to the link ID. Basically a pointer to that element in the links list. You must use “file path of” to get the actual path.

-There shouldn’t be any POSIX paths going on here so I cut that out. Also, the subroutine (without the POSIX path conversion) ended up being a single line so I brought it back up into the main routine. I don’t see a reason to have a subroutine for a single line of code that is used once.

Note that I don’t have a test file set up with links and a text file of file names so i didn’t test this. Hopefully it will work.:slight_smile:


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 "Adobe InDesign CS3"
	activate
	repeat with this_para in the_list
		set {source_file, new_name} to text items of this_para
		try
			set currentLink to (1st link of active document whose file path ends with source_file)
			
			tell application "Finder" to set name of file (file path of currentLink) to new_name
			
			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



Model: iMac Intel 10.5.5
Browser: Firefox 3.0.2
Operating System: Mac OS X (10.5)

When I run the script I get an error for each link saying “Can’t get path.” Since I know you don’t have a test doc, I thought the best way to communicate this would be to post part of the event log. This part repeats for every occurrence of the 10 links.

tell application "Adobe InDesign CS3"
	activate
	get link 1 of active document whose file path ends with "2.2B 1.eps"
		link id 303 of PostScript picture id 299 of rectangle id 270 of page id 180 of spread id 175 of document "TestDoc.indd"
end tell
tell application "Finder"
	get path
end tell
tell application "Adobe InDesign CS3"
	display dialog "Finder got an error: Can't get path." buttons {"Cancel", "OK"} default button 2 with icon 0 giving up after 5
		{button returned:"", gave up:true}
	get link 1 of active document whose file path ends with "2.2B 2.eps"
		link id 376 of PostScript picture id 372 of rectangle id 377 of page id 325 of spread id 305 of document "TestDoc.indd"
end tell

the Finder has no idea what a file path is, because it belongs to Indesign.
A method to avoid the error is to use a handler


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 "Adobe InDesign CS3"
	activate
	repeat with this_para in the_list
		set {source_file, new_name} to text items of this_para
		try
			set currentLink to (1st link of active document whose file path ends with source_file)
			my renameFile(file path of currentLink, new_name)
			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


on renameFile(thePath, theName)
	tell application "Finder" to set name of file thePath to theName
end renameFile

Thank you guys so much! The above script that Stefan posted works great.

Stefan, do you know how to make this script address duplicate files?

which kind of duplicate files?

Out of the 600 links that I am re-naming and re-linking, about 50 or so of them appear more than once in the InDesign document. Some of these just appear more than once on a page and others are on master pages.

When I run this script it doesn’t give me an error. It will re-name and re-link 1 instance of a duplicate file but all other instances of the link show up missing in the links panel.

On the first script on this thread I changed

set currentLink to (1st link of active document whose file path ends with source_file)

to

set currentLink to (every link of active document whose file path ends with source_file)

This corrected the issue on that script but I don’t know how to fix it with this shorter script.

Sorry for the ambiguity of the above question.

If I understand that right, there are many graphic items which refer to the same file.
Then it is sufficient to rename the file once and relink all appropriate links


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 "Adobe InDesign CS3"
	activate
	repeat with this_para in the_list
		set {source_file, new_name} to text items of this_para
		try
			set theLinks to (links of active document whose file path ends with source_file)
			repeat with i from 1 to count theLinks
				set currentLink to item i of theLinks
				if i = 1 then my renameFile(file path of currentLink, new_name)
				relink currentLink to source_folder & new_name
				update currentLink
			end repeat
		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


on renameFile(thePath, theName)
	tell application "Finder" to set name of file thePath to theName
end renameFile

Thanks Stefan! For those who need it I will paste the code you need for it to work in CS4.


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 "Adobe InDesign CS4"
	activate
	repeat with this_para in the_list
		set {source_file, new_name} to text items of this_para
		try
			set theLinks to (links of active document whose file path ends with source_file)
			repeat with i from 1 to count theLinks
				set currentLink to item i of theLinks
				if i = 1 then my renameFile(file path of currentLink, new_name)
				relink currentLink to file (source_folder & new_name as string)
				update currentLink
			end repeat
		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


on renameFile(thePath, theName)
	tell application "Finder" to set name of file thePath to theName
end renameFile

Hey everybody,
I made a script that is supposed to :

  1. check if there are some “jpg” files in my Indesign document
  2. open them with a Photoshop “droplet” that does lot of things (72 to 300 dPI, change ICC profile…)
  3. relink the obsolete JPG link to the newly created PSD link
  4. delete the JPG file

Here is what I did :


property imageListmodif : {"jpg", "pic", "pict", "jpeg"}

tell application "Adobe InDesign CS4"
	activate
	if (exists document 1) is false then
		tell me to activate
		set myPath to choose file with prompt "Fichiers Indesign" default location (path to desktop folder) without invisibles
		open myPath
	end if
	
	set frontDocument to the file path of front document as string
	set theFullPath to frontDocument & name of front document
	
	tell application "Finder"
		set myFolder to container of file theFullPath
		set dossierVisuels to folder "*rech_visuels" of myFolder
		set dossierImages to folder "images" of myFolder
		set dossierInfos to folder "*infos_reçues" of myFolder
		set dossierImages to dossierImages as string
		set dossierVisuels to dossierVisuels as string
		set myFolder to myFolder as string
	end tell
	
end tell


tell application "Adobe InDesign CS4"
	tell document 1
		
		repeat with oneLink in (get links)
			set linkPath to file path of oneLink
			set theLinkName to file path of oneLink as alias
			tell application "Finder" to set linkext to name extension of (linkPath as alias)
			tell application "Finder" to set theLinkName to name of (linkPath as alias)
			
			if linkext is in imageListmodif then
				try
					tell application "Finder"
						set Oldelim to AppleScript's text item delimiters
						set AppleScript's text item delimiters to "."
						set nameWithoutExt to first text item of theLinkName
						set LinktoTrash to (dossierVisuels as string) & theLinkName
						
						set PSDFile to (dossierVisuels as string) & nameWithoutExt & ".psd"
						open file linkPath using application file "MacBookPro:Users:alex:Documents:GRAPHISTE:réglages_suite_CS:psd-scripts-droplets:prepa-rech_visuels"
						
						repeat until file PSDFile exists
							delay 0.3
						end repeat
						
						tell application "Adobe InDesign CS4" to activate
						set AppleScript's text item delimiters to Oldelim
						
					end tell
					try
						relink (contents of oneLink) to (PSDFile as alias)
						update oneLink
					end try
					tell application "Finder" to move LinktoTrash to trash
				end try
			end if
		end repeat
		
	end tell
end tell

It almost does the trick, but because of the repeat loop, if there is more than one occurence of a JPG file in the document, the script wants to open it in Photoshop several times…
I think the problem is :

That’s it !
How could Indesign understand this ?
I spent 5 hours trying to find a solution tonight…
Would someone help me to sleep for the next days ?? :stuck_out_tongue:

Browser: Safari 533.18.5
Operating System: Mac OS X (10.5)

So, you have 2 issues to solve here:

  1. If one image is placed 3 times on the page, you only want to open that image once in PhotoShop.

  2. Once you save the new image, you need InDesign to relink all 3 instances of that image.

For 1, I have more a suggestion than a full solution. When I have these types of situation where you need to “de-dupe” a list, I usually sort the list so that I can repeat through it and then use something like:
if item x of list is not equal to item x-1 then go ahead and do something

For 2, I actually solved for that in one of my scripts. Basically once you are looping through the list of link names, you have to do another loop through all of the links for each link name. That will get all 3 instances (continuing my example) that have that name.

I updated your script but I didn’t have a file handy to test it. Hopefully this will get you most of the way there. Good luck!



tell application "Adobe InDesign CS4"
	tell document 1
		
		--I would get the list of links and sort it
		set theMainLinkList to (get links)
		
		--Sort the list, you can find sorting routines on this site, search for 'bubble sort' or a shell script solution
		
		repeat with oneLink from 1 to (count of theMainLinkList)
			
			--Here we check to see if it is the 1st time through the loop or that the current link is not the same name as the previous link (since the list is sorted the duplicates will always be right after each other)
			if (oneLink = 1) or (name of (item oneLink of theMainLinkList) is not equal to name of (item (oneLink - 1) of theMainLinkList)) then
				
				set linkPath to file path of (item oneLink of theMainLinkList)
				set theLinkName to file path of (item oneLink of theMainLinkList) as alias
				tell application "Finder" to set linkext to name extension of (linkPath as alias)
				tell application "Finder" to set theLinkName to name of (linkPath as alias)
				
				if linkext is in imageListmodif then
					try
						tell application "Finder"
							set Oldelim to AppleScript's text item delimiters
							set AppleScript's text item delimiters to "."
							set nameWithoutExt to first text item of theLinkName
							set LinktoTrash to (dossierVisuels as string) & theLinkName
							
							set PSDFile to (dossierVisuels as string) & nameWithoutExt & ".psd"
							open file linkPath using application file "MacBookPro:Users:alex:Documents:GRAPHISTE:réglages_suite_CS:psd-scripts-droplets:prepa-rech_visuels"
							
							repeat until file PSDFile exists
								delay 0.3
							end repeat
							
							tell application "Adobe InDesign CS4" to activate
							set AppleScript's text item delimiters to Oldelim
							
						end tell
						
						--This is where you repeat through all the links again and compare it to the current link name you are processing
						repeat with thisLinkWithThisName in links
							if name of thisLinkWithThisName = name of (item oneLink of theMainLinkList) then
								relink thisLinkWithThisName to file (PSDFile)
							end if
						end repeat
						
						--It is no longer necessary to put this in a try loop and update the link after relinking - that bug is fixed in CS4
						--try
						--relink (contents of (item oneLink of theMainLinkList)) to (PSDFile as alias)
						--update (item oneLink of theMainLinkList)
						--end try
						
						tell application "Finder" to move LinktoTrash to trash
					end try
				end if
				
			end if
			
		end repeat
		
	end tell
end tell

Again, I wasn’t able to test so hopefully there are not any major problems…

Thanks for your reply :cool:. I tried to use a “bubble sort”, but I must admit I’m not enough experienced to handle the script with it.
I started Applescript one month ago…
Would it be possible to do a repeat loop with one recurrent file paths for several links ?

Like :


repeat with (every single file path of links)
open droplet
end repeat