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!
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.
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
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!
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.
-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.
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
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.
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
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 ??
Browser: Safari 533.18.5
Operating System: Mac OS X (10.5)
If one image is placed 3 times on the page, you only want to open that image once in PhotoShop.
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!
Thanks for your reply . 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