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 […]

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