Replicating to indexed groups by means of AppleScript

In this thread in the DEVONthink To Go: Tips, Tricks & Troubleshooting forum I suggested to ‘interface’ DevonThink Pro to other applications (possibly on the iPad) by replicating the relevant entries to an indexed group and moving the corresponding documents out of the database to the corresponding folder (on, for example, Dropbox). The question was raised whether it would be possible to automate this process by means of AppleScript. This turned out to be easy.

Korm suggested to post the relevant scripts in the scripting forum, which seems a good idea.

It is easiest if the relevant entries have a unique tag. The basic idea is this:


-- specify the relevant database, tag and destination
set theDatabase to "name of your database" -- (e.g. "My Database")
set theTag to "name of the tag" -- (e.g. "Important")
set theDestination to "path to an *indexed* group"  -- (e.g. "/My Indexed Group")

tell application id "com.devon-technologies.thinkpro2"
   
   -- get the relevant references
   set theSource to lookup records with tags {theTag} in database theDatabase
   set theDestination to get record at theDestination in database theDatabase

   -- replicate the items in theSource to theDestination
   repeat with thisItem in theSource
      if not (indexed of thisItem) then replicate record thisItem to theDestination
   end repeat
   
   -- move the relevant files to the external folder
   deconsolidate record theDestination
   
end tell

An example that can be used to collect items tagged with priorities (‘High’, ‘Medium’, ‘Low’) in an indexed group “Sync DT” in the database “My Database”:


-- specify the relevant database, tag and destination
set theDatabase to "My Database"
set theTags to {"High","Medium","Low"}
set thePriorityGroup to "/Sync DT"

tell application id "com.devon-technologies.thinkpro2"

  repeat with theTag in theTags -- repeat with every tag

      -- create a string with the name of the relevant priority group
     set theDestination to thePriorityGroup & "/" & theTag
  
     -- get the relevant references
     set theSource to lookup records with tags {theTag} in database theDatabase
     set theDestination to get record at theDestination in database theDatabase
   
     -- replicate the items in theSource to theDestination
     repeat with thisItem in theSource
         if not (indexed of thisItem) then replicate record thisItem to theDestination
     end repeat
   
     -- move the relevant files to the external folder
     deconsolidate record theDestination

  end repeat -- for every tag
  
end tell

Please create the indexed “Sync DT” group before running the script. This is done as follows:

  1. Create an empty folder “Sync DT” somewhere on Dropbox/your hard disk

  2. Index that folder from within DEVONthink (File → Index).

If the relevant entries don’t have unique tags, use a smart group to collect those entries and a script of the following kind to replicate the content of the smart group to the indexed group:


-- specify the relevant database, smart group and destination
set theDatabase to "name of your database" -- (e.g. "My Database")
set theSource to "path to a (smart) group" -- (e.g. "/My Smart Group")
set theDestination to "path to an *indexed* group" -- (e.g. "/My Indexed Group")

tell application id "com.devon-technologies.thinkpro2"

	-- get references to the relevant groups
	set theSource to get record at theSource
	set theDestination to get record at theDestination
	
	-- get references to the content of the source group
	set theContent to get children of theSource
	
	-- replicate the items in the source group to the destination group
	repeat with thisItem in theContent
	   if not (indexed of thisItem) then replicate record thisItem to theDestination
	end repeat
	
	-- move the relevant files to the external folder
	deconsolidate record theDestination

end tell

For example, use the following script for the setup described in the original thread (in this setup the relevant documents are labeled “High”, “Medium” and “Low”, the references are collected in three smart groups: named ‘Label High’, ‘Label Medium’ and ‘Label Low’):


-- specify the relevant database, smart group, labels and destination
set theDatabase to "My Database"
set thePrefix to "Label " -- the prefix of the relevant smart groups
set thePriorities to  {"High","Medium","Low"} -- the relevant labels
set theDestinationGroup to "/Sync DT" -- path to the indexed group

tell application id "com.devon-technologies.thinkpro2"

	repeat with thisPriority in thePriorities -- repeat for all relevant labels
		
		 -- create strings with the relevant path names
		set theSource to thePrefix & thisPriority
		set theDestination to theDestinationGroup & "/" & thisPriority
		
		-- get references to the relevant groups
		set theSource to get record at theSource
		set theDestination to get record at theDestination
		
		-- get references to the content of the source group
		set theContent to get children of theSource
		
		-- replicate the items in the source group to the destination group
		repeat with thisItem in theContent
			if not (indexed of thisItem) then replicate record thisItem to theDestination
		end repeat
		
		-- move the relevant files to the external folder
		deconsolidate record theDestination
	   
	end repeat -- for all relevant labels

end tell

As before, create the indexed “Sync DT” group before running the script.

Thank you, Arno. I’d suggest testing this on a test database until you get the hang of it.

Hear! Hear!