Changing your tag scheme and pruning orphaned tags

I recently changed how I was storing things in my database, which left me with a bunch of items whose tags I wanted to overhaul, and a bunch of tags with no children. I wrote myself some scripts to make the process a little less tedious, so I thought I would include them here.

This script simply removes all of the tags from the selected items. I made it by modifying the included “Remove tags from selection” script.


tell application id "com.devon-technologies.thinkpro2"
	try
		set theSelection to the selection
		if theSelection is {} then error "Please select some items."
		
		set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ";"}
		repeat with theRecord in theSelection
			set tags of theRecord to {}
		end repeat
		set AppleScript's text item delimiters to od
	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

This script is attached to the tags group and moves tags to the trash if they have no children i.e. nothing is marked with that tag. I had to find a template for a “triggered” script on the forum because I don’t have an “extras” folder on the disk image I downloaded, and I don’t see anything similar in either “~/Library/Scripts” or “~/Library/Application Support/DEVONthink Pro 2”. If anyone knows where to find these scripts, let me know.


-- attach this script to the "tags" group
on triggered(theRecord)
	tell application id "DNtp"
		try
			set allTags to the children of theRecord
			
			repeat with selectedTag in allTags
				set childCount to count of (children of selectedTag)
				set thisDatabase to database of theRecord
				if childCount is 0 then
					move record selectedTag from theRecord to trash group of thisDatabase
					-- set orphanedTagCount to orphanedTagCount + 1
				end if
			end repeat
			
		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
end triggered

Thanks for the useful scripts! A future version will support such operations without having to use AppleScript and without having to manually start the operation.

Seems that the documentation isn’t up to date, there’s no Extras folder on the disk image anymore.