Experimental: "Open Or Create Annotation" in DT3 with DT2 style note's location

For my own purpose, I did a patchy modification of the main script of Annotation.templatescriptd to open the annotation note of a document (if it exists) OR to automatically create a new annotation note if it does not exists. The modified script works for me, but test it at your own risk!
(1) The new annotation note will be created in DT3 style (i.e. the URL field won’t be populated with an item link) BUT the note will be located in the same group of the annotated document (so, DT2 style).
(2) Same as the original template script, replicates of the same note will be created in all groups wherever there are replicants of the annotated document. You can just comment out the relevant code if you don’t want reps of the same note everywhere.
(3) To be safe, Annotation.templatescriptd should be duplicated, and the “main” script that needs to be modified is located in Contents/Resources/Scripts (Show Package Contents). The note template within the “Resources” folder can be modified to tailor user’s specific need. Obviously, if a user changes the placeholders, the codes will need to be changed, too.

Perhaps the final version of DT3 will take care of all of the above as standard function and this experimental script won’t be necessary.

-- Smart template adding a localized annotation for a shown document
-- Written by Eric Böhnisch-Volkmann, modified by Christian Grunenberg
-- © 2009—2016 DEVONtechnologies, LLC
-- Patchy modification by Ngan 2019.06.30

-- Open or Create New Annotation in DT3 style but putting the annotation in the same group as the annotated document

-- The non-localized default name of the new quote
property pTemplateName : "Annotation"
property pSmartGroup : "Annotations"

-- Import helper library
tell application "Finder" to set pathToAdditions to ((path to application id "DNtp" as string) & "Contents:Resources:Template Script Additions.scpt") as alias
set helperLibrary to load script pathToAdditions
set pathToLocalizedResources to my helperLibrary's pathToLocalizedResources()

	tell application id "DNtp"
		-- If a document was frontmost when activating the smart template get its item link
		if exists (content record) then
			set theFrontmostDocument to content record
		else if (count of the selection) is 1 then
			set theFrontmostDocument to item 1 of ((the selection) as list)
			error my helperLibrary's localizedString("No document shown. Please select a document, then try again.")
		end if
		set theFrontmostWindow to think window 1
		set theFrontmostDocumentURL to ("x-devonthink-item://" & uuid of theFrontmostDocument) as string
		set theFrontmostDocumentName to name of theFrontmostDocument as string
		set theRecord to (the annotation of theFrontmostDocument)
		if (the annotation of theFrontmostDocument) exists then
			open tab for record theRecord
			-- Import the predefined document
			set theTemplateFiles to my helperLibrary's pathToLocalizedResources & ((my helperLibrary's localizedString(pTemplateName) & ".rtf") as string)
			set theParents to parents of theFrontmostDocument
			set theFirstParent to item 1 of theParents
			set theRecord to import theTemplateFiles placeholders {|%documentName%|:theFrontmostDocumentName, |%documentLink%|:{|URL|:theFrontmostDocumentURL, |name|:my helperLibrary's localizedString("Click here to view annotated document")}} to theFirstParent
			-- Replicate it to the other parents if necessary
			if (count of theParents) > 1 then
				repeat with aParent in (items 2 through -1) of theParents
					replicate record theRecord to aParent
				end repeat
			end if
			-- Adjust name of the document
			set the name of theRecord to (theFrontmostDocumentName & " (" & my helperLibrary's localizedString(pTemplateName) & ")")
			set (the annotation of theFrontmostDocument) to theRecord
		end if
		-- open tab for record theRecord in think window 1
		open window for record theRecord
	end tell
on error errMsg number errNum
	display alert (localized string "An error occured when adding the annotation.") message errMsg as warning
end try