Search for docs that link to another

Is there a way to search for all documents (in my case RTF docs) that contain a link to a specific document (e.g. search for all docs that contain a x-devonthink-item link)?

Not really efficiently. In an RTF, that link is buried in the internals of the code of the file.

If ur link is named the same as the source doc, it may be easier just to search for the doc’s name.

“Perhaps” this script. It is modified from Experimental: Script to trace a backlink with a suggestion from Experimental: Script to trace a backlink. But you need to limit the search scope to a certain group (less than several hundreds of items) else it may take a few mins or more (!) to search through thousands of items. But the search will drill through all the sub-groups under the selected group. As @BLUEFROG said, it is inefficient.

Reminder: I do not recommend using the original script I post. It is more “dangerous” to use the original script IF you have not created an “empty” group for holding the results of the search. You may end up having many unwanted replicants mixing up with other items if you select the wrong destination group for holding the replicants of the search results.

-- Ngan 2019
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application id "DNtp"
	
	if selection is {} then
		display dialog ("Select a document first") buttons {"Ok"}
		return
	else if (count of selection) ≥ 1 then
		set theDoc to first item of (selection as list)
		set theDocURL to reference URL of theDoc
	end if
	
	set theSearchFrom to display group selector "Search for back link in:" for current database
	
	set theSelection to my getAllChildren(theSearchFrom) as list
	
	set theResults to {}
	repeat with theRecord in (theSelection)
		set theSource to source of theRecord
		if theSource contains theDocURL then
			set the end of theResults to theRecord
		end if
	end repeat
	
	set search results of viewer window 1 to theResults
	
end tell

on getAllChildren(theParent)
	local theList
	set theList to {}
	tell application id "DNtp"
		repeat with theChild in children of theParent
			if type of theChild is not group then
				set theList to theList & {theChild}
			else
				set theList to theList & my getAllChildren(theChild)
			end if
		end repeat
	end tell
	return theList
end getAllChildren

That’s what I was afraid of.

Those I find easy! It was looking for those links where I wasn’t consistent about the displayed text.

I’ll poke around in that script to see if there’s anything that might help. Thanks!