opening an existing annotation via UUID

Hello,

I am modifying the template script for annotations such that I only create 1 annotation per file. (This is because the URL created for the annotation links exclusively to the first annotation created.)

My very simple approach is to check whether there is a link in the URL:


if URL of theFrontmostDocument starts with "x-devonthink-item://" then

If so, I want to open this existing annotation instead of creating a new one. So creating the new one is put in the else-block, and after end-if, I open either the new annotation or the existing one with this line, that is already provided in the original script:

open window for record theRecord

So there is just one task left, getting the existing annotation into “theRecord”, and I tried it in the If-block after I checked for the item:

if URL of theFrontmostDocument starts with "x-devonthink-item://" then
			
			set theAnnotationUUID to the URL of theFrontmostDocument
set theRecord to (get record with uuid theAnnotationUUID)
			
			display dialog "Eine Anmerkungsdatei besteht schon." & return & "Öffnen?" buttons {"Abbrechen", "Öffnen"} default button 1
					else

I have tried several other solutions without the theRecord variable etc., but I just do not get this easy step done.

Thanks for any help.

Maria

Hi Maria-

I didn’t try your code, but what it looks like to me is that you are submitting a URL to “get record with uuid” when you should be submitting a UUID.

Your code tests for whether there’s a prefix of “x-devonthink-item://” on a URL such as:

But you never remove it to obtain just the UUID itself.

HTH, Charles

My code snippets were not enough for testing I suppose, I just thought that someone might have found a logical misunderstanding from the descripting.

Here is the whole code, not clean, just working harmless for the time being until I get a hint that makes it worth correcting the little glitches.

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

--	Scrambled by Maria Shinoto in 2010, strange behavior my fault


-- The non-localized default name of the new quote
property pTemplateName : "Anmerkungen"
property pAnnotationsGroup : "/Anmerkungen	"


-- 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


try
	tell application id "com.devon-technologies.thinkpro2"
		
		-- 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.")
		set theFrontmostDocumentURL to ("x-devonthink-item://" & uuid of theFrontmostDocument) as string
		set theFrontmostDocumentName to name of theFrontmostDocument as string
		
		
		(*
-- Unnecessary, when only one annotation document per document shoud exist (with links to the pages inside the annotations)
-- 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
*)
		
		
		-- Create the Notes group, if necessary
		set theAnnotationsGroupRecord to create location (my helperLibrary's localizedString(pAnnotationsGroup))
		if theAnnotationsGroupRecord is missing value then error my helperLibrary's localizedString("Could not create the 'annotations' group.")
		
		-- Check for existing annotation and open
		
		if URL of theFrontmostDocument starts with "x-devonthink-item://" then
			
			set theAnnotationUUID to the URL of theFrontmostDocument
			set theRecord to (get record with uuid theAnnotationUUID)
			
			
			display dialog "Eine Anmerkungsdatei besteht schon." & return & "Öffnen?" buttons {"Abbrechen", "Öffnen"} default button 1
			
			
			
			display dialog theAnnotationUUID & return & theRecord buttons {"OK"} default button 1
			
			(*
here I get missing data for theRecord, this why it does not work below. 
*)
			
			
			
			
		else
			
			-- Import the predefined document
			set theTemplateFiles to helperLibrary's pathToLocalizedResources() & ((my helperLibrary's localizedString(pTemplateName) & ".rtf") as string)
			set theRecord to import theTemplateFiles placeholders {|%documentName%|:theFrontmostDocumentName, |%documentLink%|:{|URL|:theFrontmostDocumentURL, |name|:my helperLibrary's localizedString("Click here to view annotated document")}} to theAnnotationsGroupRecord
			
			-- Adjust name of the document
			
			-- ..After checking whether document exists: set the name to the search pattern
			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 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
					set URL of theFrontmostDocument to ("x-devonthink-item://" & uuid of theRecord) as string
					display dialog "Die ursprüngliche URL wurde in den Kommentar kopiert. " & return & "Die URL weist zur Anmerkung." buttons {"OK"} default button 1
				end if
			else
				set URL of theFrontmostDocument to ("x-devonthink-item://" & uuid of theRecord) as string
			end if
			
			
		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

Thanks for any advice.

Maria

Hi Maria-

Please re-read my post. I do believe I found your trouble:

You reference a URL when you need to reference a UUID.

You must remove “x-devonthink-item://” from the string held by the variable “theAnnotationUUID.”

HTH, Charles

Charles,

now I understand what you mean, thanks for making it clear. I will try and come back to the forum later.

Best,
Maria

Hi Maria-

As both the URI and the UUID are a constant number of characters, I think that you could just do something like:

or something…

HTH, Charles

Charles,

Thanks for the hint. You were right – I was just looking for text delimeters and character extraction. (Somehow I really do not get along with AppleScript’s specifications, I never find what I search for…) With a constant length of the string, “56” is OK though, that helps!

Maybe that today I won’t be able to present a ready template, but on Tuesday for sure.

(We are moving to the mountains for the hottest part of the summer).

Best and thanks again!

Maria

Sounds like what my Mom and her husband are currently doing in Arizona, escaping from Sun City to Prescott during what would be a few intolerable months in the former. :slight_smile:

As the length might vary (e.g. archived emails have a different UUID), …


(characters 21 thru -1 of theAnnotationUUID) as string

…should work as long as there are no additional parameters (e.g. “?page=5”).

Thanks for your reply. Unfortunately, there still seems to be something wrong in my way of understanding. Here is what seems to be the crucial part of the code, with your additions as far as I understood:

			set theAnnotationUUID to texts from (characters 21 thru -1 of theAnnotationUUID) as string
			set theRecord to (get record with uuid theAnnotationUUID)

This does not get “translated”, I cannot save the script because “as” cannot follow “)”, I am told.

My second problem is: Do these lines get the document?

Thanks again,
Maria

This snippet should work:


         set theAnnotationUUID to (characters 21 thru -1 of theAnnotationUUID) as string
         set theRecord to (get record with uuid theAnnotationUUID)

If not, then just post your current script and I’ll have a look at it.

Thanks for looking into the problem. It still exists, and I get error messages of the same kind when I have the script perform with an existing annotation file (i.e. when I want the script to open the annotation).

The script:

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

--	Scrambled by Maria Shinoto in 2010, strange behavior my fault


-- The non-localized default name of the new quote
property pTemplateName : "Anmerkungen"
property pAnnotationsGroup : "/Anmerkungen	"


-- 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


try
	tell application id "com.devon-technologies.thinkpro2"
		
		-- 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.")
		set theFrontmostDocumentURL to ("x-devonthink-item://" & uuid of theFrontmostDocument) as string
		set theFrontmostDocumentName to name of theFrontmostDocument as string
		
		
		(*
-- Unnecessary, when only one annotation document per document shoud exist (with links to the pages inside the annotations)
-- 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
*)
		
		
		-- Create the Notes group, if necessary
		set theAnnotationsGroupRecord to create location (my helperLibrary's localizedString(pAnnotationsGroup))
		if theAnnotationsGroupRecord is missing value then error my helperLibrary's localizedString("Could not create the 'annotations' group.")
		
		-- Check for existing annotation and open
		
		if URL of theFrontmostDocument starts with "x-devonthink-item://" then
			
			set theAnnotationURL to the URL of the theFrontmostDocument
			set theAnnotationUUID to (characters 21 thru -1 of theAnnotationURL) as string
			set theRecord to (get record with uuid theAnnotationUUID)
			
			display dialog "Eine Anmerkungsdatei besteht schon." & return & "Öffnen?" buttons {"Abbrechen", "Öffnen"} default button 1
			
			
			
			display dialog theAnnotationUUID & return & theRecord buttons {"OK"} default button 1
			
			(*
here I get missing data for theRecord, this why it does not work below. 
*)
			
			
			
			
		else
			
			-- Import the predefined document
			set theTemplateFiles to helperLibrary's pathToLocalizedResources() & ((my helperLibrary's localizedString(pTemplateName) & ".rtf") as string)
			set theRecord to import theTemplateFiles placeholders {|%documentName%|:theFrontmostDocumentName, |%documentLink%|:{|URL|:theFrontmostDocumentURL, |name|:my helperLibrary's localizedString("Click here to view annotated document")}} to theAnnotationsGroupRecord
			
			-- Adjust name of the document
			
			-- ..After checking whether document exists: set the name to the search pattern
			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 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
					set URL of theFrontmostDocument to ("x-devonthink-item://" & uuid of theRecord) as string
					display dialog "Die ursprüngliche URL wurde in den Kommentar kopiert. " & return & "Die URL weist zur Anmerkung." buttons {"OK"} default button 1
				end if
			else
				set URL of theFrontmostDocument to ("x-devonthink-item://" & uuid of theRecord) as string
			end if
			
			
		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

Best,
Maria
Bildschirmfoto 2010-08-02 um 22.26.14.png

Just remove the line…


display dialog theAnnotationUUID & return & theRecord buttons {"OK"} default button 1

…and it’s working.

O lala, this was meant to control the variables…

It works!

One more question: It seems that one can relate a short cut with the template (not using the system preferences), but I do not get how to do it. Is there a special way to get shortcuts with templates?

Maria

Just append the shortcut to the filename after 3 underscores, e.g. MyTemplate___Cmd-Shift-Ctrl-Opt-X.templatescriptd

OK – and thank you for your help,

Maria