Append "backlinks" to RTF or Text document

(original post superseded by the post below)

Updated the script so that it only returns backlink references that are RTF, RTFD, or Text documents. It also does not return a self-reference to the selected document itself.


-- NOTE: this script will modify the contents of your file, has not been tested with your data, and most likely can destroy something that matters to you
-- do NOT use this script other than for testing
tell application id "com.devon-technologies.thinkpro2"
	set theDatabase to database
	set theSource to the first item of (the selection as list)
	set thisType to the kind of theSource
	if thisType is in {"RTF", "RTFD", "Text"} then
		set theQuery to the name of theSource
		set theResults to search theQuery in theDatabase within text
		if theResults is not {} then
			set theReferences to "Backlinks -- "
			set foundLink to 0
			repeat with thisResult in theResults
				if the reference URL of thisResult is not equal to the reference URL of theSource then
					if the kind of thisResult is in {"RTF", "RTFD", "Text"} then
						set theReferences to theReferences & the name of thisResult & " "
						set foundLink to foundLink + 1
					end if
				end if
			end repeat
			if foundLink is 0 then
				set theReferences to theReferences & "<<no backlinks found>>"
			end if
			try
				set sTxt to plain text of theSource
				set sP to every paragraph of sTxt as list
				set nP to ""
				repeat with tP in sP
					if tP does not contain "Backlinks --" then
						if tP does not contain "" then
							set nP to nP & tP & return
						end if
					end if
				end repeat
				set plain text of theSource to nP & return
				make new paragraph with data theReferences at the end of text of think window 1
				save think window 1
			end try
			
		else
			display dialog "There were no documents containing " & "\"" & theQuery & "\""
		end if
	else
		display dialog "The selected document is not RTF, RTFD or Text"
	end if
end tell

edit: May 28, 2012 @ 08:42 PM this version contains a rudimentary routine to remove existing “backlinks” references from the file. The script can be modified to be used as a triggered script.

Thanks for creating this script!

One caveat: the script will link all instances of files wherein the named item appears. Accordingly, if your item is a common and/or recurring term throughout the database, you will get lots of ‘backlinks’ which technically aren’t backlinks. E.G. if you have an item named “Smith” the script will pull and list as a back link any other item which contains the word “Smith.” In other words, the script doesn’t test to see if the item name as it appears in other areas of the database is, in fact, a wikilink. So, best practice to get truly intentional links is to use unique item identifiers, e.g. SmithJones. Additionally, if you have identified an alias or aliases for the wikiterm, the script will not pick up on those as backlinks.

Even with those caveats, I’m really enjoying the capability and thanks again for scripting out a solution!

pete

No, technically, they are backlinks. The script finds instances of a document’s name in other RTF, RTFD, or Text files in the same database. That’s how DEVONthink’s so-called “wiki links” work. Whether that link in the other document was intentionally put there or not, is not relevant, known, or knowable to DEVONthink, which merely connects the two documents with a clickable link.

Coding such a test is no small matter. There is no simple method in DEVONthink to do this, thus the script uses “search”. DEVONthink isn’t managing sophisticated links in the manner of Tinderbox or even VoodooPad, which are built around linking - so don’t expect more out of this script than it can provide.

A possible addition to the script for anyone who wishes to have at it. This script was a toy proof of concept, not intended to be any more than that. NOTE: this script will modify the contents of your documents, has not been tested with your data, and most likely can destroy something that matters to you. Do NOT use this script other than for experimentation. Use it for what it is.