InDesign Re-link Choose File and auto-keystroke filename to select

InDesign CS5, when re-linking an image, you are taken to a Choose File dialog box. If you are faced with a large list of files, you type the first few characters of the needed file name to select the first matching name.

I’d really like to learn a way to do this: When presented with the Choose File dialog, send keystrokes of the existing link’s file name as a way of selecting the closest matching file name. I couldn’t figure a way to make it happen.

Any ideas or other help would be appreciated. (I’m not a very advanced Applescripter.) Thanks.


--with an InDesign picture box already selected
set defaultFolder to (choose folder) -- (just for this example)

tell application "Adobe InDesign CS5"
	tell document 1
			set thePath to file path of item link of graphic 1 of selection
		on error
			set thePath to file path of item link of selection
		end try
		set olddelim to AppleScript's text item delimiters
		set AppleScript's text item delimiters to ":"
		set thename to last text item of (thePath as text)
		set AppleScript's text item delimiters to ""
		set characterCount to ((count of characters of thename) - 4)
		set thename to (characters 1 thru characterCount) of thename as string
		set AppleScript's text item delimiters to olddelim
		relink item link of graphic 1 of selection to (choose file with prompt "Choose image " & thename of type {"TIFF", "JPEG", "EPS"} default location defaultFolder without invisibles)
		tell application "System Events" to keystroke (every character of thename) --to select the first close match in list
	end tell
end tell

Model: Mac Pro
Browser: Firefox 7.0.1
Operating System: Mac OS X (10.6)

I don’t know about that, I am not even sure if GUI scripting can do that (but I don’t do much GUI scripting honestly).

What I’ve done in a similar script is relink files. If the same file name isn’t found, then I get the first couple characters of the original file name (maybe the first 5 or so) and then get the first file in that relink folder (using Finder or system events) whose name starts with those first characters. I show the user a dialog that says “Missing file XXX, relink to file XXY?” then the user can accept and it will relink to that, or skip relinking that file altogether. If you are really creative I guess you could try to search for the next file that starts with those 5 characters. That all depends on how your files are named, if they are all different, or you have some kind of numbering system or date-naming system. So maybe you want to match the first 12 characters of a file name etc.

It’s been said before often, but trying to script things in the UI is tough and often unreliable. I really avoid it in all but the most impossible situations.

Thanks SuperMacGuy. The files come from all over and base names can be 2-50+ characters long.

I’ll probably do as you’ve described and be all set.