Searching for items containing the same Alias?

Sure :slight_smile:

This Smart Rule script finds all records whose alias(es) are used in other record’s aliases and adds a tag.

It can be used as a normal script or as a Smart Rule script:

  • If used as normal script it acts on the current database.

  • If you want to use it in a Smart Rule set property theDatabaseName.

Properties:

  • theDatabaseName: set database name
    Only necessary if you want to use it as Smart Rule script

  • considerCase: whether aliases should be compared case sensitive

  • theParentTag: name of the tag under which all other tags are grouped.
    The tag is created automatically

  • theTagSuffix: text that is added to the tag’s name

-- Tag records whose alias(es) are used in other record's aliases

property theDatabaseName : "" -- set database name (only necessary if you want to use it as Smart Rule script)
property considerCase : false -- whether aliases should be compared case sensitive
property theParentTag : "_Aliases Need Action" --  name of the tag under which all other tags are grouped. The tag is created automatically.
property theTagSuffix : "_AliasNeedsAction" -- text that is added to the tag's name

on run
	tell application id "DNtp"
		set theDatabaseName to name of current database
		my performSmartRule()
	end tell
end run

on performSmartRule()
	tell application id "DNtp"
		try
			if considerCase = true then
				considering case
					my processAliases()
				end considering
			else
				my processAliases()
			end if
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end performSmartRule

on processAliases()
	tell application id "DNtp"
		try
			set theDatabase to database theDatabaseName
			set theDatabase_Aliases to aliases of contents of theDatabase whose aliases ≠ ""
			set theDatabase_Aliases_string to my tid(theDatabase_Aliases, ",")
			set theDatabase_Aliases_list to my tid(theDatabase_Aliases_string, {", ", "; ", ",", ";"})
			
			set theDatabase_Aliases_processed to {}
			
			repeat with thisAlias in theDatabase_Aliases_list
				set thisAlias to thisAlias as string
				
				if thisAlias is not in theDatabase_Aliases_processed then
					set theResults to search "aliases:~" & thisAlias in root of theDatabase
					set theResults_Count to (count theResults)
					
					if theResults_Count > 1 then
						set theResults_filterd to {}
						
						repeat with i from 1 to theResults_Count
							set thisRecord to item i of theResults
							set thisRecord_Aliases to my tid(aliases of thisRecord, {", ", "; ", ",", ";"})
							if thisRecord_Aliases contains thisAlias then set end of theResults_filterd to thisRecord
						end repeat
						
						set theResults_filterd_Count to (count theResults_filterd)
						
						if theResults_filterd_Count > 1 then
							repeat with i from 1 to theResults_filterd_Count
								set thisRecord to item i of theResults_filterd
								set tags of thisRecord to (tags of thisRecord) & {theParentTag & "/" & thisAlias & theTagSuffix}
							end repeat
						end if
						
					end if
				end if
				set end of theDatabase_Aliases_processed to thisAlias
			end repeat
			
		on error error_message number error_number
			activate
			if the error_number is not -128 then display alert "Error: Handler \"processAliases\"" message error_message as warning
			error number -128
		end try
	end tell
end processAliases

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