InDesign list of links to MSWord, or preferably Excel

Hello, again. It appears that a good portion of this code is descended from previous posts”some by newbies”from a decade ago. No offense to anyone past or present, but the central InDesign parts aren’t very concise or efficient. There’s also some redundant bits.

This:

is equivalent to the line immediately before it”“set source_folder…””as it produces the same alias(es); the previous line’s logic is also confusing, as the variable and prompt reference a folder, when it actually concerns files.

My approach is a ground-up rewrite.

set aliasTarget to choose file with prompt "Select InDesign files to scavenge their links." with multiple selections allowed without invisibles
set stuff to {}

repeat with aFile in aliasTarget
	set text item delimiters to tab
	tell application "Adobe InDesign CS3"
		set docTarget to open aFile
		tell docTarget to repeat with target in all graphics
			tell target to set stuff's end to {parent's parent's name, "h: " & horizontal scale & "  v: " & vertical scale} & item link's {name, file path, link type, status} & return as text
		end repeat
		close docTarget
	end tell
end repeat

set my text item delimiters to ""
tell application "Finder" to write (stuff as text) to ((make file with properties {name:"List of Linked files.txt"} at desktop) as alias)

© 2015, Marc Anthony. All rights reserved.

I’m using the script below & getting this error:
error “Adobe InDesign CC 2017 got an error: Invalid object for this request.” number 30614.
Can’t figure it out why in some documents it works & the others don’t. Also is it possible keep writing on the same text file? If I running it again I have to delete previous text file.

set aliasTarget to choose file with prompt "Select InDesign files to get a list of the link names in a text file." with multiple selections allowed without invisibles

set stuff to {}

repeat with aFile in aliasTarget
	set text item delimiters to tab
	tell application "Adobe InDesign CC 2017"
		set user interaction level of script preferences to never interact
		
		set docTarget to open aFile
		set DocName to (name of active document as string) & return
		
		tell docTarget to repeat with target in all graphics
			tell target to set stuff's end to item link's {name} & DocName as string
			
		end repeat
		close docTarget saving no
		set user interaction level of script preferences to interact with all
		
	end tell
end repeat

set my text item delimiters to ""
tell application "Finder" to write (stuff as text) to ((make file with properties {name:"XXXX files.txt"} at desktop) as alias)

Model: iMac
AppleScript: Version 2.10 (194)
Browser: Safari 537.36
Operating System: macOS 10.14

There are some desirable modifications, however, none account for the intermittent behavior, other than the potential for the code to allow non-ID file types to be chosen. You’ve also made functional changes which no longer necessitate some features from the original example. Remove all lines referencing TIDs, all coercions, and the brackets around item link’s name, as that single item is already a text. If you want to keep appending to a file, then you have to open/close access and write to EOF; use StefanK’s example from post #20.

set aliasTarget to choose file of type "IDd5" with prompt "Select InDesign files to get a list of the link names in a text file." with multiple selections allowed without invisibles --enforces ID file types

set stuff to ""

repeat with aFile in aliasTarget
	tell application "Adobe InDesign CS3"
		set user interaction level of script preferences to never interact
		set docTarget to open aFile
		tell docTarget to repeat with targ in all graphics
			tell targ to set stuff to stuff & item link's name & " " & docTarget's name & return
		end repeat
		close docTarget saving no
		set user interaction level of script preferences to interact with all
	end tell
end repeat

Thanks Marc for the suggestion. I discovered why the script was stopping
→ error “Invalid object for this request.” number 30614.

There are images that are not linked. They were dragged & dropped from Photoshop or Illustrator hence they will not show in the links palette. I’m adding the try error statement but I want to continue if the error happens?

set aliasTarget to choose file of type "IDd8" with prompt "Select InDesign files to get a list of the link names in a text file." with multiple selections allowed without invisibles --enforces ID file types
set stuff to ""
repeat with aFile in aliasTarget
	tell application "Adobe InDesign CC 2017"
		set user interaction level of script preferences to never interact
		set docTarget to open aFile
		tell docTarget to repeat with targ in all graphics
			try
				tell targ to set stuff to stuff & item link's name & " " & docTarget's name & return
				
			on error number 30614
				--display dialog "Ignore error & continue"
			end try
		end repeat
		close docTarget saving no
		set user interaction level of script preferences to interact with all
	end tell
end repeat