Tags not applied in order specified in script

I want to sort records’ tags so that I can efficiently scan them.

I set up a test database that has Exclude groups from tagging enabled. It has three groups, A, B, and C. These Groups have their Group name specified in the Tags section as Tag to be applied in their Info dialog.

I have a record in Group C that is tagged with the tags: C, X, V, E, and A, applied in this order.

C is a tag that resulted from the record residing in a Group named “C.” This Group has the Tag “C” specified in its Info dialog.

Of the other tags, A is another Group tag, and the rest are “ordinary” tags, not associated with any Group.

The code below runs without error.

-- get the record's tags, sort them, and reapply the sorted list of tags.

tell application id "com.devon-technologies.thinkpro2"
	try
		set theRecs to selection
		repeat with rec in theRecs
			set theTags to tags of rec
			my showTags(theTags)
			
			set theTags to my simpleSort(theTags)
			my showTags(theTags)
			
			set tags of rec to {}
			set tags of rec to theTags
			
			set theTags to tags of rec
			my showTags(theTags)
		end repeat
	on error errmsg number errnum
		if the errnum is not -128 then display alert "DEVONthink Pro" message errmsg as warning
	end try
end tell


on showTags(tagList)
	set str to ""
	repeat with t in tagList
		set str to str & t & " "
	end repeat
	display dialog str
end showTags


-- thanks to http://www.macosxautomation.com/applescript/sbrt/sbrt-05.html
-- (modified for clarity)
on simpleSort(theList)
	set the indexList to {}
	set the sortedList to {}
	repeat (the number of items in theList) times
		set the lowItem to ""
		repeat with ctr from 1 to (number of items in theList)
			if ctr is not in the indexList then
				set thisItem to item ctr of theList as text
				if the lowItem is "" then
					set the lowItem to thisItem
					set the lowItemIndex to ctr
				else if thisItem comes before the lowItem then
					set the lowItem to thisItem
					set the lowItemIndex to ctr
				end if
			end if
		end repeat
		set the end of sortedList to the lowItem
		set the end of the indexList to the lowItemIndex
	end repeat
	return the sortedList
end simpleSort

The Events and Replies in the AppleScript editor shows:

tell application "DEVONthink Pro"
	get selection
		--> {content id 116 of database id 14}
	get tags of content id 116 of database id 14
		--> {"C", "X", "V", "E", "A"}
end tell
tell application "AppleScript Editor"
	display dialog "C X V E A "
		--> {button returned:"OK"}
	display dialog "A C E V X "
		--> {button returned:"OK"}
end tell
tell application "DEVONthink Pro"
	set tags of content id 116 of database id 14 to {}
	set tags of content id 116 of database id 14 to {"A", "C", "E", "V", "X"}
	get tags of content id 116 of database id 14
		--> {"C", "A", "E", "V", "X"}
end tell
tell application "AppleScript Editor"
	display dialog "C A E V X "
		--> {button returned:"OK"}
end tell
Result:
{button returned:"OK"}

All indications are that the list of tags has been sorted and passed to DTPO. I would expect that after deleting the record’s tags, that the new list of tags would be applied in the order specified. But, the sorted list of A, C, E, V, X is not applied. Instead, this list is applied: C, A, E, V, X.

It appears that the Group Tag of the Group in which the record was created is applied first, despite the sorted order of the tags.

Moreover, this code works as expected if Enable groups from tagging is not selected in the database properties. That is, the sorted list is actually applied.

This seems like a bug to me in DTPO’s application of Tags to records.

Any thoughts? Thanks.

DEVONthink sorts tags in the Tags bar group tags alphabetically.

A bug is usually something that’s not working as designed. I don’t think that’s the case, here. The behavior you see is the design with respect to the display of tags in the Tags bar.

Within the database, the “tags” property of a document record is a list whose members are stored in the order the tag was created. So, your script, by clearing the tags, sorting them, and then re-storing them in the “tags” property list should be accomplishing what you want. It’s just that the Tags bar always sorts alphabetically. What happens in the database and what happens in the UI are, in this case, two different things – each of which is internally consistent with its particular design.