Just got ChatGPT to write a script to edit tags

Pretty nifty. Pops up a menu of existing tags, asks which you want to delete, then asks what tags you’d like to add.

tell application id "DNtp"
	set theSelection to the selection
	if theSelection is {} then
		display alert "Please select an item."
		return
	end if
	
	set theRecord to item 1 of theSelection
	set existingTags to the tags of theRecord
	
	if existingTags is not {} then
		set tagsToDelete to choose from list existingTags with prompt "Select tags to delete:" with multiple selections allowed and empty selection allowed
		if tagsToDelete is false then
			-- User cancelled
			return
		end if
		
		-- Remove selected tags from existing tags
		set newTags to {}
		repeat with t in existingTags
			if t is not in tagsToDelete then
				set end of newTags to t
			end if
		end repeat
	else
		-- No existing tags
		set newTags to {}
	end if
	
	-- Prompt for new tags to add
	set newTagsToAdd to text returned of (display dialog "Enter new tags to add, separated by commas:" default answer "")
	
	-- Split the input into a list of tags
	set AppleScript's text item delimiters to ","
	set newTagsList to every text item of newTagsToAdd
	set AppleScript's text item delimiters to ""
	
	-- Clean up each tag by trimming whitespace
	set cleanedNewTagsList to {}
	repeat with t in newTagsList
		set trimmedTag to my trimText(t)
		if trimmedTag is not "" then
			set end of cleanedNewTagsList to trimmedTag
		end if
	end repeat
	
	-- Combine the remaining existing tags with the new tags
	set finalTags to newTags
	repeat with t in cleanedNewTagsList
		if t is not in finalTags then
			set end of finalTags to t
		end if
	end repeat
	
	-- Update the item's tags
	set the tags of theRecord to finalTags
end tell

-- Handler to trim whitespace from a string
on trimText(t)
	set t to t as string
	set trimmedText to t
	repeat while trimmedText begins with " " or trimmedText begins with tab
		set trimmedText to text 2 thru -1 of trimmedText
	end repeat
	repeat while trimmedText ends with " " or trimmedText ends with tab
		set trimmedText to text 1 thru -2 of trimmedText
	end repeat
	return trimmedText
end trimText

neat! why do you have to delete first instead of either Add or Delete button?

what did you type on ChatGPT? lol

it might help me with this

Why do you need a script to add/delete tags assigned to a record?
The Devonthink UI seems straightforward

I actually use a script to assign tags.
It presents selected lists of tags and subtags using “choose from list”
The first tag list is a set of Type tags
Subsequent tag lists are assembled based on the selected Type
For example Type-Receipt also get tags for Vendor and Budget-Category

perhaps that would be better. mind sharing the script for assigning tags

You have the basic elements in your script

First step is to prepare theTagList by adding specific tags
for example a set of Type tags

set theTagList to …
set theTags to (choose from list theTagList with prompt "Specify Tags" with multiple selections allowed)
if theTags is not false then set finalTags to finalTags & theTags
...
-- Update the item's tags
	set the tags of theRecord to finalTags

thank you

Would love to see your ChatGPT prompt for this is you don’t mind sharing it.