Updating indexed groups across many databases

Note: This initial script was only the starting point and should not be used. Please scroll down to find Christian’s script.

I am maintaining a few databases, and all of them contain at least several indexed groups (folders). The folders that these indexed groups point to get frequently modified outside of DT. Whenever one visits on of these groups, indexed items are updated on the fly. However, there are two situations where this is not good enough: (1) Search and (2) Synching to DevonThinkToGo. So far, I have kept my indexed groups updated periodically by manually going through all databases, and using the “File > Update Indexed Items” command. Even with my 5 databases, this gets tiring. Below is a script that globally udpates all indexed groups across all my databases. I used GUI scripting to accomplish this. To call up all the databases, I use the workspaces (see Go menu). I save top-level views of all my databases as workspaces. The script can then call them one by one, using the key shortcuts (CMD-OPT-n, where n is an integer).

-- 24-Mar-2014, GG
-- This script goes through all my databases and updates their index folders; this is useful to update all indexed files prior to a sync to DevonThinkToGo on the iPad, e.g. when getting ready for a trip. Without this script I have to sync each database by hand, by going to the top level, selecting all group, and using "File > Update Indexed Items".
-- Implementation: Each database is saved as a "Workspace", such that the top level group is displayed (see Menu "Go - Workspaces"). Once they are created, they can be accessed by the shortcuts "CMD-OPT-n", where "n" is an integer. We then loop through these workspaces using GUI scripting, select all groups at the top level, execute the "Update Indexed Items" command, and then deselect, and move to the next database (i.e. workspace).

tell application id "DNtp" to activate

-- set the workspace numbers for all the databases to be updated
set work_space_list to {"1", "2", "3", "4", "5"}

tell application "System Events"
	tell process "DEVONthink Pro Office"
		repeat with work_space_item in work_space_list
			-- GUI scripting of the key combo to call up a workspace
			keystroke work_space_item using {option down, command down}
			tell menu bar 1
				tell menu bar item "Edit"
					tell menu "Edit"
						click menu item "Select All"
					end tell
				end tell
				tell menu bar item "File"
					tell menu "File"
						click menu item "Update Indexed Items"
					end tell
				end tell
				tell menu bar item "Edit"
					tell menu "Edit"
						click menu item "Deselect All"
					end tell
				end tell
			end tell
			delay 1 -- add a delay if desired to follow by eye what's happening
		end repeat
		-- at the end of the sequence, move back to displaying the inbox (workspace 6)
		keystroke "6" using {option down, command down}
	end tell
end tell

One thing I haven’t figured out yet: When I run this script from the Applescript Editor, it nicely executes and I can visually follow all the database switching, selecting etc. When I copy the script into the DT script directory, it executes, but I don’t see the visual progression; but at the end, the updating seems to have worked.
Correction: Calling the script through the script menu does not seem to work reliably. Any suggestions?

Here’s a script that updates all currently opened databases without using user interface scripting:

-- Update indexed items of all databases
-- Created by Christian Grunenberg on Tue Mar 25 2014.
-- Copyright (c) 2014. All rights reserved.

-- Set to true if indexed items are not only located at the root of the database
property pAllRecords : false

tell application id "DNtp"
		set theDatabases to databases
		show progress indicator "Updating Indexed Items" steps (count of theDatabases)
		repeat with theDatabase in theDatabases
			step progress indicator (name of theDatabase as string)
			set theRecords to records of theDatabase -- Children of root
			repeat with theRecord in theRecords
				if pAllRecords or theRecord is indexed then
					synchronize record theRecord -- Updates children of theRecord too
				end if
			end repeat
		end repeat
		hide progress indicator
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

Criss, thanks for the script! Could you go into more detail as to what happens when this is set to true or false?

Thanks! This is interesting.

If it’s true then all records are checked & updated, that’s a lot slower. But false is sufficient if all indexed groups are located at the root.

Maybe add this to the Support Assistant > Scripts?

Nice! The non-GUI version works faster. Looking at the code, it is also clear that one cannot come up with such a script without some deeper insights into the DT dictionary.

Nevertheless, some good came out of my GUI script: It triggered someone with insights to write the real thing :smiley:

From my point of view, this is a very useful script. In fact I have yet another application for it: I am just going through a long list of indexed pdf files that need to be put through OCR. Currently, I much prefer Acrobat for doing this. I open the file out of DT with Acrobat, run the OCR, and if I like the result, I can simply save and I’m done. However, DT will not update the information on this file until indexed items are updated (e.g. the just OCR’ed file will not disappear from the smart group showing all the un-ocr’ed files, and the concordance is not available). With Christian’s new script, it is easy to have everything updated.

I think this script should be included in the standard list of DT scripts.

Nice script, thanks. How can it be included to automatically open the log window after updating?