Example: Add or Remove Tags for a To-Do in Things 3

Here is a bit of AppleScript that will add a given tag (in this example the tag is :house:home, emoji included!) to a to-do in Things 3 or remove it if it is already present for the selected to-do(s).

To modify it for your use, change :house:home below to whatever tag you'd like, but leave the preceding comma and space as they are.

Place this text in an Execute AppleScript Action:

tell application "Things3"
	set thisTag to ", 🏠home"
	repeat with selectedToDo in selected to dos
		set tagNames to tag names of selectedToDo
		if tagNames contains thisTag then
			my findAndReplaceInText(tagNames, thisTag, "")
			set tag names of selectedToDo to result
		else if tagNames does not contain thisTag then
			set tagNames to tagNames & thisTag
			set tag names of selectedToDo to tagNames
		end if
	end repeat
end tell

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

An improved version that reads a bit nicer (a gift to future cfriend) and does not require touching the AppleScript to change tags.

Example: Add or Remove Tags for a To-Do in Things 3 -- v2

Example: Add or Remove Tags for a To-Do in Things 3.kmmacros (4.3 KB)

And the AppleScript:

set inst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"
	set thisTag to getvariable "Local__thisTag" instance inst
end tell

tell application "Things3"
	--prepend the comma and space that makes the rest work nicely
	set thisTag to ", " & thisTag
	repeat with selectedToDo in selected to dos
		set currentTags to tag names of selectedToDo
		if currentTags contains thisTag then
			my findAndReplaceInText(currentTags, thisTag, "")
			set tag names of selectedToDo to result
		else if currentTags does not contain thisTag then
			set newTags to currentTags & thisTag
			set tag names of selectedToDo to newTags
		end if
	end repeat
end tell

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText
1 Like