Script: Add tag alias without punctuation

I just stumbled over this script - it’s really useful!

To automate even further, I modified the script slightly to work in smart rules.

-- Add alias without punctuation to every tag of the current database

-- Setup, optional:  	By default all punctuation is removed. If you want to restrict removal of punctuation set property theIgnoredPunctuation to e.g. ",".
--						Don't include a space " " in property theIgnoredPunctuation. Don't do this: ", " (or ", ;"). This is correct: "," (or ",;").

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property theIgnoredPunctuation : "" -- Add punctuation that should be removed. Don't include a space, i.e. don't set ", " (or ", ;"). This is correct: "," (or ",;"). Set to an empty string "" to remove all punctuation.
property theAliasesDelimiter : ";" -- Set your preferred delimiter, either "," or ";". Don't include a space, i.e. don't set ", " (or "; "). This is correct: "," (or ";").

on performSmartRule(theRecords)
	if theIgnoredPunctuation ≠ "" then
		set theCharacterSet to current application's NSMutableCharacterSet's alloc()'s init()
		theCharacterSet's addCharactersInString:(theIgnoredPunctuation)
		set theCharacterSet to current application's NSCharacterSet's punctuationCharacterSet()
	end if
	tell application id "DNtp"
			set theTags to tag groups of current database
			show progress indicator "Adding Aliases... " steps (count theTags) as string with cancel button
			repeat with thisTag in theTags
				set thisTag_Name to name of thisTag
				step progress indicator thisTag_Name
				set thisTag_Name_withoutPuncation to my stripPunctuation(thisTag_Name, theCharacterSet)
				if thisTag_Name_withoutPuncation ≠ thisTag_Name then
					set thisTag_Aliases to aliases of thisTag
					set thisTag_Aliases_list to my tid(thisTag_Aliases, {", ", "; ", ",", ";"})
					if thisTag_Name_withoutPuncation is not in thisTag_Aliases_list then
						set thisTag_Aliases_list_new to thisTag_Aliases_list & {thisTag_Name_withoutPuncation}
						set thisTag_Aliases_new to my tid(thisTag_Aliases_list_new, theAliasesDelimiter)
						set aliases of thisTag to thisTag_Aliases_new
					end if
				end if
			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" message error_message as warning
		end try
	end tell
end performSmartRule

on stripPunctuation(theText, theCharacterSet)
		set theString to (current application's NSString's stringWithString:theText)
		set theArray to theString's componentsSeparatedByCharactersInSet:(theCharacterSet)
		set theString_withoutPunctuation to theArray's componentsJoinedByString:""
		return theString_withoutPunctuation as string
	on error error_message number error_number
		display alert "Error: Handler \"stripPunctuation\"" message error_message as warning
		error number -128
	end try
end stripPunctuation

on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	if class of theInput = text then
		set theOutput to text items of theInput
	else if class of theInput = list then
		set theOutput to theInput as text
	end if
	set AppleScript's text item delimiters to d
	return theOutput
end tid

1 Like