Automatically append iAnnotate annotations to pdf in DT Pro

Hello,

First of all thanks to all scripters who shared their code here and on the web. I’m new to Applescript and still rely on you (and google) to get things done.

I’m using the DTTG-iAnnotate combo to read and annotate pdfs on my ipad. I would like to have something similar to the Annotation-template in that my annotations are automagically linked to the actual pdf.

iAnnotate can send the annotation by email in the following format.

I can create a new record, set the URL of the record to the annotated paper but then I have to do some cut and paste… I tried to modify (change code/placeholder) the Annotation template but without success.

Input, direction, codes would be greatly appreciated!

Mercuriade

Here’s my naive Frankenstein script.

-- select email with appropriate annotation
tell application "Mail"
	activate "Mail"
	set theSelectedMessages to selection
	set theMessage to item 1 of theSelectedMessages
	set theContent to content of theMessage
end tell

-- massage annotation to get meaningful titles
try
	set prevTIDs to AppleScript's text item delimiters
	set theString to theContent
	
	set AppleScript's text item delimiters to "Highlight (Orange)" --word you want to replace
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Results" --the replacement word
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	
	--Make a link to DT Pro file
	set AppleScript's text item delimiters to "Annotation Summary for: "
	set temp to every text item of theString
	set AppleScript's text item delimiters to "x-devonthink-item://"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
end try

set theLien to paragraph 1 of theString


tell application id "com.devon-technologies.thinkpro2"
	activate
	set theJeton to create record with {name:"iAnnonotation", content:theString, type:rtf, URL:theLien}
	
end tell


In case anyone is interested.

-- =============================================
-- Raison d'être: create a link annotations↔annotated pdf. Annotations are emailed from iAnnotate. pdf is in DTTG and sync with DT Pro. The script also gives a meaningful name to the different type of annotation.

-- Workflow: Open DevonThink to Go's pdf in iAnnotate. Annotate and email annotations alone. Select email (or have a Mail.app rule) and run script. 

-- Credits: Workflow based on a post by Walton Jones @ www.drosophiliac.com. Code largely inspired by annotation templates in Devonthink.

-- Disclaimer: use at your own risk…

-- Version 0.1 (09/30/2012)
-- =============================================

tell application "Mail"
	activate "Mail"
	
	--extract annotations from email
	
	set theSelectedMessages to selection
	set theMessage to item 1 of theSelectedMessages
	
	set theContent to content of theMessage
	
end tell

try
	
	--massaging the content of the email to get meaningful categories for the annotations
	set prevTIDs to AppleScript's text item delimiters
	set theString to theContent
	
	set AppleScript's text item delimiters to "Highlight (Orange)" --word you want to replace
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Techniques" --the replacement word
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Highlight (Green)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Références"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Highlight (Yellow)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Résultats importants"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Highlight (Custom Color: #bf00bf)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "En lien avec nous"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Highlight (Green)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Référence"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Highlight (Red)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Sommaire important"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Underline (Green)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Référence d'intérêt"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Underline (Yellow)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Résultats d'intéret"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Underline (Orange)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Technique d'intéret"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	set AppleScript's text item delimiters to "Line Drawing (Black)"
	set temp to every text item of theString
	set AppleScript's text item delimiters to "Commentaire écrit"
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
	
	--Make a link to DT Pro file
	set AppleScript's text item delimiters to "Annotation Summary for: "
	set temp to every text item of theString
	set AppleScript's text item delimiters to "x-devonthink-item://"
	--set AppleScript's text item delimiters to ""
	
	set theString to temp as string
	set AppleScript's text item delimiters to prevTIDs
end try


--getting what amounts to the uuid of the annotated pdf.
set pdfuuid to paragraph 1 of theString


--actually create the link between annotation and cited pdf.
tell application id "com.devon-technologies.thinkpro2"
	activate
	
	--create a record with the annotation and make a link to the cited pdf 
	set poupoune to create record with {name:"iPadAnnotation", content:theString, type:rtf, URL:pdfuuid}
	
	set pouid to uuid of poupoune
	--URL  de poupoune
	set thepoupouneUrl to ("x-devonthink-item://" & pouid) as string
	
	
	-- make the url to the cited pdf
	set thepdfUrl to ("x-devonthink-item://" & pdfuuid) as string
	set theRecord to (get record with uuid pdfuuid)
	--open window for record theRecord --uncomment to actually see the window
	set URL of theRecord to thepoupouneUrl
	
end tell

Not clear from the post, and without parsing the two scripts you posted side-by-side, I assume the second one posted is an update to first one you posted. Yes?

If so, are you looking for specific comments on the second script? It isn’t an easy script to test outside of your own setup because it is so specific to the way you have arranged the data. Though it looks like an interesting concept, it isn’t easily portable for other other users – especially those without scripting experience.

I’d suggest you could significantly simplify the long section that searches and replaces items if you defined as a property up front a list of two-element records. For example:


property listFindReplace : {{"find a", "replace a"}, {"find b", "replace b"}} as list

with that, you could condense the parsing code into a much smaller routine. You could eventually supplant the definition of listFindReplace with another small routine that reads these find/replace pairs from an external plain text file. Having done that, then the script is easily portable for other users.

A portion that I don’t see working is this

--getting what amounts to the uuid of the annotated pdf.
set pdfuuid to paragraph 1 of theString

The reference URL (which is based on a record’s UUID) is not embedded in the first paragraph of a PDF. (But I might be reading the code wrong; it isn’t easy to follow in its current state.) Have you tested this segment of the code — and do you find that it works? Is the result you find when you inspect the value of pdfuuid exactly the same as you get when you select the PDF record in DEVONthink and use Edit > Copy Item Link. I’d be surprised if they are, but pleased to see you’ve discovered a hidden new feature if they were :stuck_out_tongue:

Since it takes some time to configure a test bed to test the code you published … is it complete? Or are there problems?

Thanks korm,

Comments are welcomed: that’s how I’m going to learn!

The second script is based on the first one, but actually the first functional version.

Well, sorry about that. I put pieces together as I was learning.

Thanks for the insights!! I knew there was something less tacky but when I was writing multiple text item delimiters together, only the last delimiter was changed… I’ll have to look more closely on how to use an external plain text file as I have absolutely no idea where to begin with.

The trick here is that when I open a DTTG pdf in iAnnotate, the name of the pdf corresponds to the uuid. That’s what I taking advantage of. When I email the annotation from iAnnotate, the name of the pdf is on the first line of the email.

[/quote]

[/quote]
After erasing "Annotation Summary for: " I can then “set pdfuuid to paragraph 1 of the string.”

I used ‘paragraph’ because I was unable to get it to work with ‘character’.

In my limited testing, this script works. Now I really do have to read some pdfs… :wink: