Convert this Group to an Indexed Group (updated)

This script will export a selected group and its children. It will then index that exported group. The process can be used to convert a group that is internal to the database into an indexed group.

Why would you want to do this? It is helpful when rearranging documents or group hierarchies in the the database. If you have a collection of groups and files some of which are indexed and some are not, and you want to reorganize, a helpful method is to move everything into the database with the “Move into Database” command, then do your housekeeping, and then use this script to put the new hierarchy back to disk and index that hierarchy back to DEVONthink.

To use this:

  1. Select a group – a group with child groups is OK
  2. Run the script
  3. You will be prompted for a file system destination for that hierarchy of group(s) and files
  4. The script will export the hierarchy to that location
  5. You will next be prompted to choose which parent to index
  6. That folder and its children will be indexed at a location in your database that has the same parent as the group you chose in step 1 – the reason for this is that the indexed group will then be a sibling of the group selected in step 1

The group selected in step 1 and the indexed folder resulting from step 6 are identical – one hierarchy is in the database and one is hierarchy is external and indexed. The group from step 1 is usually left in place and not deleted. However, there is property at the head of the script “deleteOrMove” which can be set to “true” (the default is “false”). If that property is true then a feature is activated that opens a dialog asking if you want to delete or move the original step 1 group. If you choose to move that group, you are prompted for a destination in any of your open databases. This latter feature can be helpful if you are reorganizing your data among different databases.

You will notice copies of a file “DEVONtech_storage” that appear in folders that contain data exported from DEVONthink. These contain metadata that DEVONthink is interested in – though not used or useful in the case of this script. The script does not delete these files, because they belong to you, but it is safe to delete them if you choose.

[size=85]Before using this script on a group, I recommend manually turning off group-tagging for the group and for its child groups. Select the group and make sure “Exclude from Tagging” is checked. This step will avoid a possible issue with Mavericks tagging. After you finish converting a group to an indexed group you can restore group tagging if you wish. I might update the script in the future to automate this precaution.[/size]

(* 20131122.2*)
property deleteOrMove : false

tell application id "com.devon-technologies.thinkpro2"
	try
		set theSelection to the first item of (selection as list)
		if the kind of theSelection is not "Group" then error "Please select a group"
		
		set theParent to the first parent of theSelection
		set theChildren to (every child of theSelection) as list
		
		set x to choose folder with prompt "Select a folder"
		
		export record theSelection to x
		
		set x to choose folder with prompt "Choose parent folder to index" default location x
		indicate x to theParent
		
		if deleteOrMove then
			set theName to the name of theSelection
			set theQuestion to "Delete or move the original (non-indexed) group at" & return & theName
			set theAnswer to button returned of (display dialog theQuestion buttons {"Delete Original", "Move Original", "Cancel"} default button 3 cancel button 3 with title "Delete or Move?")
			if theAnswer is "Delete Original" then
				set theResult to delete record theSelection
			else if theAnswer is "Move Original" then
				set theResult to move record theSelection to display group selector
			else
				return
			end if
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell


[size=80]There was a request downstream in this thread to include logic to optionally replace the external folder if it existed before this script was run. I’ve examined the logic to support this and concluded that there would be too many alternatives to make this a stable and understandable processes. Just a simple example: what if there are more than one group in (one or more databases) with the same name or the same content? Then what is being replaced? How to know? I think this is computationally intractable, but would welcome suggestions. [/size]

Korm… I just popped up a reply in one of the DTP/BE threads, bemoaning my decision to have duplicated, rather than indexing everything… And now I find this! ^^

Bookmarked. Big thank you. And enough with the Jedi-mind-tricks now. :wink:

Thanks. Please start slowly with this and experiment with a couple of groups without deleting the originals in your database until you are satisfied it is working as you expect.

Please also see the recommendation in my posting above to temporarily turn off group tagging when using this script

Will do. Have a dummy database with odds and ends in, that I experiment on. Won’t do anything drastic before cloning the drive/backing-up etc. Thanks for the heads-up on the tagging.

Great work! I believe you’ve helped me with my tagging issue.

But, one thing. How would I modify the script so that I could have the option of overwriting an existing group/folder when I export?

I tried “replacing true” which I recall could be used with “move” but it didn’t work.

I don’t know if there’s anything that would export only new and/or changed DTP items, but for my uses just occasional overwriting would be more foolproof and easier to accomplish.

Not sure I understand what this means, or why do it?

If you want to add internal documents to an external group that’s indexed, then just put the documents into the indexed group and use Move to External Folder from the contextual menu.

Overwriting is when you save/move/export to a location that has a file/folder with the same name and you overwrite it. If you don’t have the option, you get “existing folder” and existing folder-1," instead of just “existing folder.”

If I write an Applescript using “move,” this can be done without getting an alert asking if one wants to replace the existing files/folders, but I couldn’t discover something similar for “export.”

The reason is that your script works more efficiently. And “update index items” isn’t working for me now, so until it’s fixed exporting and then reimporting does the trick.

Thanks for writing it.

I was going to write one for my own use, and was inquiring to see if you knew how to modify it to allow the overwrite.

Thanks for your help; I’ll look elsewhere as you suggest.

Does this script respect/preserve the custom data fields of DT3?

This script basically performs File > Export > Files & Folders… and File > Index… afterwards. The metadata is retained but the UUID (e.g. used by item links) isn’t.

I also think this script (ancient) is made obsolete by DEVONthink 3. I would never need it anymore.