Experimental: Script to trace a backlink

While it’s easy to insert the backlink of an item to other items in DT, it is not as easy to trace which documents have the backlink of a particular item.

In the process of coding other stuff, I have written this utility script to “forward-trace” the item-link of a selected document/item.
(1) Select an item that you want to trace its item link
(2) the script will ask for a group where you search for all documents that may contain the backlink. I strongly suggest not to choose a “database”, it will take a long time to find “a backlink” in every item of the entire database! (It takes about 1.5 min to trace through a group fo 300 text files looking for a backlink)
(3) the script will ask for, or create, a group for the results; all documents that contain the backlink of (1) will be replicated in that group.

PS: (1) perhaps someone can consider modifying the script to become a smart rule triggered script. This arrangement may allow a user to drag and drop an item onto the smart rule for getting the result.
(2) I haven’t vigorously debugged the script, but it works well in my database.
(3) If you are using wikilinks as backlink, or you always use the full name of the source document as the name of the backlink, it’s much faster and more straight-forward to conduct a normal search in DT (by searching the name of backlink in content only).

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

-- ngan 2019.08.11
-- script to retrive all documents that contain the backlink of an item and replicate the results to a destinated group

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 saveTo to display group selector "Save the results to:" for current database
	
	repeat with theRecord in theResults
		replicate record theRecord to saveTo
	end repeat
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

Thanks for the script! Are these files Markdown documents and do they link to online resources? Otherwise this should be faster.

Hi.
I later discovered that it might be the Script Debugger that is slowing down the process. I ran the script in debug mode, and I am guessing that SD is sending request (“tell”?) to DT each time (for tracking the variable theRecord?) in the repeat loop. When the script is running native in DT, the speed is more like 15-20 secs.

BTW: Instead of replicating the results to a group you could also set the search results of the frontmost main window:

	set search results of viewer window 1 to theResults

Interesting… Regardless of the fact that I am searching within a script with a criteria that is not available in normal search method? (there is no predicates for item link in normal or advance search as far as I am aware).

Thank you again.

This is especially intended for scripts which extend the integrated search criteria.

:clap::clap::clap::+1::+1::+1::smiley::smiley::smiley: