Export DEVONthink Pro Records to Personal Brain

Description

This script and description is reprinted from the original post by "korm" at the DEVONthink Pro Scripting Forum. He's a hell of an AppleScripter -- and this program should help and inspire people working with projects using XML files! - Justin This script is for those who might be interested in exporting DTPO records to create "thoughts" in PersonalBrain. The script operates on a selection of records (selection can be discontinuous), creates a single thought for each DTPO record, and an "attachment" for each thought that is the custom URL linking that thought back to the DTPO record. In other words, click on the Thought in PersonalBrain and the record in DTPO will open. A nice benefit of this, for PB users, is that one can select documents from multiple databases by using DTPO Search or a global SmartGroup and combine them under a single Thought in PB. The script creates an XML file compliant with the PersonalBrain DTD. I tested this with PersonalBrain 6 (beta), but the DTD hasn't changed since version 5.5, and should be honored in that version too. Do you own testing, though. You would Import the file as type "Brain XML".

The Code

-- Export selection as thoughts in PersonalBrain XML
-- Each thought has an attachment - link back to the DTPO record
-- Incorporates a handler written by C Turner
-- This is unlicensed code; use at your discretion and at your own risk
-- The author is not responsible for the code or the result of using it: warning, you may lose your data

tell application id "com.devon-technologies.thinkpro2"
   try
      set this_database to current database
      set these_items to the selection
      if these_items is {} then error "Please select some contents"
      set this_file_name to choose file name with prompt "Export" default name (name of this_database as string) & ".xml"
     
      (*show progress indicator "Creating PersonalBrain xml..."*)
     
      set allThoughts to ""
      set allAttachments to ""
      set allEntries to ""
     
      repeat with this_item in these_items
         
         set theName to my XMLify(name of this_item)
         set theUUID to my XMLify(uuid of this_item)
         
         set thoughtGUID to do shell script "uuidgen"
         set attachmentGUID to do shell script "uuidgen"
         set attachmentEntryGUID to do shell script "uuidgen"
         
         set thisThought to "<Thought>" & return
         set thisThought to thisThought & "<guid>" & (thoughtGUID as string) & "</guid>" & return
         set thisThought to thisThought & "<name>" & theName & "</name>" & return
         set thisThought to thisThought & "<label>DTPO Item</label>" & return
         set thisThought to thisThought & "<creationDateTime></creationDateTime>" & return
         set thisThought to thisThought & "<realModificationDateTime></realModificationDateTime>" & return
         set thisThought to thisThought & "<displayModificationDateTime></displayModificationDateTime>" & return
         set thisThought to thisThought & "<activationDateTime></activationDateTime>" & return
         set thisThought to thisThought & "<linksModificationDateTime></linksModificationDateTime>" & return
         set thisThought to thisThought & "<isType>0</isType>" & return
         set thisThought to thisThought & "<color>0</color>" & return
         set thisThought to thisThought & "<accessControlType>0</accessControlType>" & return
         set thisThought to thisThought & "</Thought>" & return
         
         set allThoughts to allThoughts & thisThought & return
         
         set thisAttachment to "<Attachment>" & return
         set thisAttachment to thisAttachment & "<guid>" & (attachmentGUID as string) & "</guid>" & return
         set thisAttachment to thisAttachment & "<attachmentEntries>" & return ¬
            & "<attachmentEntryID>" & (attachmentEntryGUID as string) & "</attachmentEntryID>" & return ¬
            & "</attachmentEntries>" & return
         set thisAttachment to thisAttachment & "<objectID>" & (thoughtGUID as string) & "</objectID>" & return
         set thisAttachment to thisAttachment & "<name>" & my XMLify(uuid of this_item as string) & "</name>" & return
         
         -- the attachmentType for DTPO custom URLs is 3 = web site
         set thisAttachment to thisAttachment & "<attachmentType>3</attachmentType>" & return
         
         set thisAttachment to thisAttachment & "<location>" & my XMLify("x-devonthink-item://" & (uuid of this_item as string)) & "</location>" & return
         set thisAttachment to thisAttachment & "<dataLength>0</dataLength>" & return
         set thisAttachment to thisAttachment & "<format></format>" & return
         set thisAttachment to thisAttachment & "<creationDateTime></creationDateTime>" & return
         set thisAttachment to thisAttachment & "<modificationDateTime></modificationDateTime>" & return
         set thisAttachment to thisAttachment & "</Attachment>" & return
         
         set allAttachments to allAttachments & thisAttachment & return
         
         set thisEntry to "<Entry>" & return
         set thisEntry to thisEntry & "<guid>" & (attachmentEntryGUID as string) & "</guid>" & return
         set thisEntry to thisEntry & "<EntryObjects>" & return ¬
            & "<EntryObject>" & return ¬
            & "<objectType>0</objectType>" & return ¬
            & "<objectID>" & (thoughtGUID as string) & "</objectID>" & return ¬
            & "</EntryObject>" & return ¬
            & "</EntryObjects>" & return
         set thisEntry to thisEntry & "<body></body>" & return
         set thisEntry to thisEntry & "<creationDateTime></creationDateTime>" & return
         set thisEntry to thisEntry & "<modificationDateTime></modificationDateTime>" & return
         set thisEntry to thisEntry & "</Entry>" & return
         
         set allEntries to allEntries & thisEntry & return
         
      end repeat
     
      set this_xml to "<?xml version="1.0" encoding="UTF-8"?> " & return
      set this_xml to this_xml & "<!DOCTYPE BrainData SYSTEM "http://www.thebrain.com/dtd/BrainData1.dtd">" & return
      set this_xml to this_xml & "<BrainData>" & return
      set this_xml to this_xml & "<Thoughts>" & return & allThoughts & return & "</Thoughts>" & return
      set this_xml to this_xml & "<Entries>" & return & allEntries & return & "</Entries>" & return
      set this_xml to this_xml & "<Attachments>" & return & allAttachments & return & "</Attachments>" & return
      set this_xml to this_xml & "</BrainData>" & return
     
      (*hide progress indicator*)
     
      set theFile to open for access this_file_name with write permission
      if (get eof of theFile) > 0 then
         set eof of theFile to 0
      end if
      set this_xml to this_xml as «class utf8»
      write this_xml as «class utf8» to theFile
      close access theFile
   on error error_message number error_number
      (*hide progress indicator*)
      if the error_number is not -128 then
         try
            «event panSdisA» "DEVONthink Pro" given «class mesS»:error_message, «class as A»:warning
            (*
on error number error_number
            if error_number is -1708 then «event panSdlog» error_message given «class btns»:{"OK"}, «class dflt»:1
*)

         end try
      end if
   end try
end tell

(* The following handler was written by C Turner *)

on XMLify(str)
   set numChars to length of str
   set escapedStr to ""
   repeat with i from 1 to numChars
      set char to character i of str
      if char is equal to ">" then
         set escapedStr to escapedStr & "&gt;"
      else if char is equal to "<" then
         set escapedStr to escapedStr & "&lt;"
      else if char is equal to "&" then
         set escapedStr to escapedStr & "&amp;"
      else if char is equal to """ then
         set escapedStr to escapedStr & "
&quot;"
      else if char is equal to (ASCII character 10) then
         set escapedStr to escapedStr & "
&#10;"
      else if char is equal to (ASCII character 9) then
         set escapedStr to escapedStr & "
&#9;"
      else
         set escapedStr to escapedStr & char
      end if
   end repeat
   return escapedStr
end XMLify