Link Renamer

I’m working on a catalog of jewlry where the links are difficult to dicern. For example this is the way the links show up in the finder:
CT57440DV.sct
CT57440BBV.sct
CT57440E_BV.sct
CT57440EV.sct
CT57440C_BV.sct
CT57440E_B.tif
CT57440DDV.sct
CT57440A_B.tif
CT57440A_BV.sct
CT57440BV.sct
CT57440F_B.tif
CT57440C_B.tif
CT57440F_BV.sct

It would be useful in both QuarkXpress and Indesign, you could click on a photo, select rename and it actually renames the link in finder and updates the link reference in the layout program. If I was to rename the link first, both programs would lose their link, reporting that the link is “missing”. I would then have to manually relink all the changed links. If it could be done in the layout, it would be less frustrating.

What would be also cool is telling the layout doc to match all the links on each page, move them to sub folders named Page001, Page002, Page003, etc. and update them to their new folders.

Try this:

tell application "Adobe InDesign CS3"
	set myDoc to active document
	tell myDoc
		try
			set myContent to {image, PDF, EPS}
			set mySelection to item 1 of selection
			--set myClass to class of mySelection
			set myLink to item link of mySelection
			set myLinkName to name of myLink
			
			if class of mySelection is in myContent then
				display dialog "Enter a new name for the selected item:" default answer myLinkName
				set the newName to the text returned of the result
				tell myLink
					set myFileName to file path of myLink
					tell application "Finder"
						set the source_folder_path to my extract_parent_folder_path_from(myFileName)
						set name of file myFileName to newName
						set the newPath to the source_folder_path & the newName as string as alias
					end tell
					relink myLink to newPath
					set myLink to update myLink
				end tell
			end if
		on error
			display dialog "Please select placed image before running this script."
			return
		end try
	end tell
end tell

on extract_parent_folder_path_from(the_filepath)
	set the_filepath to the_filepath as text
	set x to the offset of ":" in (the reverse of every character of the_filepath) as string
	set the_filepath to (characters 1 thru -(x) of the_filepath) as text
end extract_parent_folder_path_from

Thanks podclock,

Works great!

I noticed that you must select the image with the direct selection tool rather than the Selection tool.

I’m going to add that notice to the error text.
I’m also going to create a loop to do all the images in a doc.

You could also test for the class and skip the try. This should work:

tell application "Adobe InDesign CS3"
	set myDoc to active document
	tell myDoc
		--try
		set myContent to {image, PDF, EPS}
		set mySelection to item 1 of selection
		set myClassList to {oval, rectangle, polygon}
		set myClass to class of mySelection
		if myClass is in myClassList then
			set myImage to all graphics of item 1 of mySelection
			set myLink to item link of item 1 of myImage
			set myLinkName to name of myLink
			set mySelection to item 1 of myImage
		else
			set myLink to item link of mySelection
			set myLinkName to name of myLink
		end if
		
		if class of mySelection is in myContent then
			display dialog "Enter a new name for the selected item:" default answer myLinkName
			set the newName to the text returned of the result
			tell myLink
				set myFileName to file path of myLink
				tell application "Finder"
					set the source_folder_path to my extract_parent_folder_path_from(myFileName)
					set name of file myFileName to newName
					set the newPath to the source_folder_path & the newName as string as alias
				end tell
				relink myLink to newPath
				set myLink to update myLink
			end tell
		end if
		--on error
		--display dialog "Please select placed image before running this script."
		--return
		--end try
	end tell
end tell

on extract_parent_folder_path_from(the_filepath)
	set the_filepath to the_filepath as text
	set x to the offset of ":" in (the reverse of every character of the_filepath) as string
	set the_filepath to (characters 1 thru -(x) of the_filepath) as text
end extract_parent_folder_path_from