Make an Annotation with Links, Notes, Tags v2

This had been superseded by later more robust versions. See later in thread

Here is an updated version of Korm’s script which allows for multiple annotations to the same document to be easily referenced.

Clicking the URL field of annotated document will bring up a separate tag view containing all the documents which have annotated the original document. Create a tag called “annotations” in the tags folder before using the script

Its based on Korm’s ideas in this thread:
[url]manually cross-linking & adding rich text notes to items?]


(* Original script by Korm

modified by Frederiko using Korm's ideas for multiple annotations using tags: https://discourse.devontechnologies.com/t/manually-cross-linking-adding-rich-text-notes-to-items/17667/12


Usage:
Create a tag called "annotations" in the tags folder. All the tags will be stored underneath that tag avoiding tag bloat.
*)

property noDecoration : "\" style=\"text-decoration:none;"
property preferRTF : true

tell application id "DNtp"
		
		(*
         Select a single document
      *)
		set thisItem to content record of think window 1
		set thisItemUUID to (uuid of thisItem) as string
		
		(*
         Get selected citation text
      *)
		--set theCitedText to selected text of think window 1
		--if theCitedText is "" then error "Please select some text"
		
		set theCitedText to the (selected text of think window 1 as string)
		if theCitedText is "" then error "Please select some text"
		
		
		
		(*
         Create a prefix for the annotation otherwise use the name of this item
         Use case: create a text annotation in the document with the same text as the prefix
         You will find the prefix on the clipboard so add it immediately after this dialog ends
      *)
		set thePrefix to text returned of (display dialog "Annotation Prefix:" with title "Annotation Prefix" default answer "")
		if thePrefix is not "" then
			set theName to thePrefix & " " & the name of thisItem
			tell application "System Events"
				set the clipboard to (thePrefix as text)
			end tell
		else
			set theName to the name of thisItem
		end if
		
		(*
         Get a note for this clipping
      *)
		set theNote to text returned of (display dialog "Notes for this clipping:" with title "Notes" default answer "[none]")
		
		(*
         Get tags for the annotation document
      *)
		repeat
			set theTagPrompt to display name editor "Add Tags" info "Tags (separated by semicolons):"
			if theTagPrompt is not "" then exit repeat
		end repeat
		
		
		(*
		Adds the uuid of the document and adds it as a tag to the annotation document
		*)
		
		try
			
			set annotationURL to (the reference URL of (the child named thisItemUUID of (the tag group named "annotations" of the current database)))
		on error
			
			set theRecord to (create record with {name:thisItemUUID, type:group} in tag group named "annotations" of current database)
			set annotationURL to the reference URL of theRecord
			
		end try
		
		set theTagPrompt to (theTagPrompt & ";" & thisItemUUID)
		
		(* Get a name for the annotation document
      *)
		set theAnnotationName to text returned of (display dialog "Name for this annotation file" with title "Annotation File Name" default answer (thePrefix & ": " & the name of thisItem))
		
		(*
         Make a text-search link for this citation
      *)
		if number of words of theCitedText is greater than 5 then
			set maxWords to 5
		else
			set maxWords to number of words of theCitedText
		end if
		set thisWord to 1
		set theCitedTextText to ""
		repeat while thisWord is less than or equal to maxWords
			set theCitedTextText to theCitedTextText & word thisWord of theCitedText
			set thisWord to thisWord + 1
			if thisWord is less than or equal to maxWords then
				set theCitedTextText to theCitedTextText & "%20"
			end if
		end repeat
		set searchCitationLink to " (<a href=\"" & (the reference URL of thisItem as string) & "?search=" & theCitedTextText & noDecoration & "\">" & " Text " & "</a>)"
		
		(*
         Make a path link for this document
      *)
		set thePathLink to " (<a href=\"file://" & the path of thisItem & noDecoration & "\">" & " File " & "</a>)"
		
		(*
         Make a page link for this page if the document is a pdf
      *)
		if the current page of think window 1 ≠ -1 then
			set pageNumber to ((the current page of think window 1) as string)
			-- 20110212 adjust page number to +1 for printing purposes
			set printPageNumber to " (Pg. " & (((the current page of think window 1) + 1) as string) & ")"
			set thePage to "?page=" & pageNumber
			-- set clickHere to "(Click here - page " & pageNumber & ")"
			set clickHere to "| Page " & pageNumber & " link"
		else
			set thePage to ""
			set clickHere to "| Page Link "
			set printPageNumber to "<b><i> (no page - source is not PDF)</i></b>"
		end if
		set theURL to (the reference URL of thisItem as string) & thePage
		set thePageLink to " (<a href=\"" & theURL & noDecoration & "\">" & " Page " & "</a>)"
		
		(*
         Compile all these elements and prepare the annotation
      *)
		set theAnnotation to "<p><b>" & thePrefix & "</b> " & thePageLink & searchCitationLink & thePathLink & "</p>" & "<p>" & theCitedText & printPageNumber & "</p>" & "<b><i>Notes:</i></b><br>" & "<p>" & theNote & "</p>" & "<b><i>Tags:</i></b><br>" & "<p>" & theTagPrompt & "</p>"
		
		if preferRTF then
			set o_theAnnotation to (do shell script "echo " & quoted form of theAnnotation & " | textutil -format html -convert rtf -stdin -stdout")
		else
			set o_theAnnotation to theAnnotation
		end if
		
		
		(*
         Create the annotation document
      *)
		set theAnnotationDocument to create record with {URL:theURL, name:theAnnotationName, source:o_theAnnotation, type:rtf} in display group selector
		
		(*
         Add the tags to the annotation document
      *)
		set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ";"}
		set theTags to text items of theTagPrompt
		set tags of theAnnotationDocument to (parents of theAnnotationDocument) & theTags
		set AppleScript's text item delimiters to od
		
		(*
		Sets the URL of the document to the reference URL of the tag. 
		*)
		
		set URL of thisItem to annotationURL
		
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
	
end tell

[edit] Script now tags all annotations in the “annotations tag” so the tag folder is not burdened by hundreds of pointless tags. You should never have to go to the annotations tag because you would invariably get to the relevant tag when you click the annotated documents URL field
[edit] Removed reliance on having selected item highlighted. This could cause inconsistent results when using the back button. Now the annotated document is always derived from the active windo

Frederiko