Is there a way to put items w/tags automatically into groups

I put all of my items into the Global Inbox. I tag them as to which of my 5 databases that they belong to. From each tag I move them by bulk into the inbox of that database. Is there a way to do that automatically?

From the individual database, can I do a similar process, except the end result would be one of the groups or subgroups inside that database?

Thanks for any help.

The following script moves the selected items to the desired databases, assuming that the first tag is identical to the name of the destination database:


tell application "DEVONthink Pro"
	set theSelection to the selection
	repeat with theRecord in theSelection
		set theTags to tags of theRecord
		set theName to item 1 of theTags
		set theDatabase to database named theName
		try
			if theDatabase is not missing value then move record theRecord to (incoming group of theDatabase)
		end try
	end repeat
end tell