Append selected text to rich text file (Annotation Template)

Hello,

I have been playing around with the annotation template.

My question now is, I have selected text in a pdf.
This selected text should be appended to the annotation where it reads
“Type your note here.” initially.

So my question has several sub questions.
In the template script, I can access the selected text like this

		set selText to the selected text of think window 1

Around here, I can imagine that the selText could be added to the file:


set theRecord to import theTemplateFiles placeholders {|%documentName%|:theFrontmostDocumentName, |%documentLink%|:{|URL|:theFrontmostDocumentURL, |name|:my helperLibrary's localizedString("Click here to view annotated document")}} to theFirstParent

But I’m not certain.

My overall goal is to have a new template, which links excerpts (i.e., direct citations) of a pdf to that pdf. In contrast to the annotation template, which creates only one annotation, I’d like to have as many files linked to the pdf as I create excerpts.
That piece already works by deleting the section dealing with existing annotations in the annotation template script.

Any suggestions would be highly appreciated.

This piece

set oldText to the rich text of theRecord
		set the rich text of theRecord to oldText & selText

nearly does the trick.
Only, it removes all layout and formatting from the annotation.
Any ideas, how to keey the style of the annotation?

The whole script now is:

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


-- 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 "com.devon-technologies.thinkpro2" as string) & "Contents:Resources:Template Script Additions.scpt") as alias
set helperLibrary to load script pathToAdditions
set pathToLocalizedResources to my helperLibrary's pathToLocalizedResources()


try
	tell application id "com.devon-technologies.thinkpro2"
		set selText to the selected text of think window 1
		
		-- If a document was frontmost when activating the smart template get its item link
		set theFrontmostDocument to content record
		
		set theFrontmostWindow to think window 1
		if (theFrontmostDocument is missing value) then error my helperLibrary's localizedString("No document shown. Please select a document, then try again.")
		-- this is the devon think Identifyier
		set theFrontmostDocumentURL to ("x-devonthink-item://" & uuid of theFrontmostDocument) as string
		-- this is the name of the document to which an annotation should be added
		set theFrontmostDocumentName to name of theFrontmostDocument as string
		
		
		
		-- Add smart group if necessary: this seems to create a smart group called /Annotations
		set pSmartGroupLocalized to my helperLibrary's localizedString(pSmartGroup)
		set pSmartGroupLocalizedPath to ("/" & pSmartGroupLocalized) as string
		
		if not (exists record at pSmartGroupLocalizedPath) then
			set theSmartGroupFile to my helperLibrary's pathToLocalizedResources & pSmartGroup
			set theSmartGroupRecord to import theSmartGroupFile to root of current database
			if theSmartGroupRecord is not missing value then
				set the name of theSmartGroupRecord to pSmartGroupLocalized
			end if
		end if
		
		
		
		-- Add page parameter if a current page attribute is available for this record
		if the current page of theFrontmostWindow ≠ -1 then
			set theFrontmostDocumentURL to theFrontmostDocumentURL & "?page=" & ((the current page of theFrontmostWindow) as string)
		end if
		
		-- 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) & ")")
		
		-- Finally, add the note's item link to the annotated document's URL field if possible
		if type of theFrontmostDocument is not bookmark and type of theFrontmostDocument is not feed then
			if URL of theFrontmostDocument ≠ "" then
				set theComment to comment of theFrontmostDocument
				if theComment ≠ "" then set theComment to theComment & return
				set theComment to theComment & my helperLibrary's localizedString("Original URL") & ": " & (URL of theFrontmostDocument)
				set comment of theFrontmostDocument to theComment
			end if
			set URL of theFrontmostDocument to ("x-devonthink-item://" & uuid of theRecord) as string
		end if
		
		-- get old rich text
		set oldText to the rich text of theRecord
		set the rich text of theRecord to oldText & selText
		-- 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

Rich text scripting of Mac OS X is limited to visible views/windows, otherwise the formatting isn’t preserved. Therefore you’d have to open a window for the record, modify its text and save it.

Thanks for that information.
Is it possible to do that with a script.
I know how to open the record in a window via script. But is it possible to add text to the window and save it?

Thanks

Perhaps this helps. The snippet below is from a personalized script I developed for selecting and copying text in a pdf and adding it to the end of an annotation document (RTF). As I understand it, this is similar to what you’re looking to do. You’ll need to sort out the bits of this snippet that refer to local variables in my script, but you should get the general drift by examining the snippet. (I won’t be posting my whole script as it contains actions specific to my research.)

[size=85]Code Snippet - for example only; not a working routine


if theQuotationText ≠ "" then
		make new paragraph with data (return & "Quotation: ") at the end of text of think window 1
		make new word with data ("Page " & the theCurrentPageNumber & return) with properties {URL:theQuotationFullUrl} at the end of text of think window 1
		bold last paragraph of text of think window 1
		
		make new paragraph with data (theQuotationText & return & return) at end of text of think window 1
		italicize last paragraph of text of think window 1
		
		make new paragraph with data ("Annotation:" & return) at the end of text of think window 1
		bold last paragraph of text of think window 1
	end if
```[/size]

This would be super-helpful if it were a working script. Can you post a working snippet for those of us who don’t know AppleScript so well?