Thanks, Shane, that seems to work great, in both Sierra 10.12.6 and Mojave 10.14.4! Below is my script based on your code, which allows me to select a combination of files and folders and tag them all with the selected tags.
Interestingly, I did find a way to use keyboard shortcuts to invoke the Tag box (weirdly, you go to Finder/Preferences/Tags, delete all custom tags, and then specify your keyboard shortcut), which has the benefit of allowing you to choose the order of the tags. See https://apple.stackexchange.com/questions/339126/is-there-a-way-to-set-a-finder-shortcut-for-tags-in-macos-10-14-mojave/339177.
Also thanks to CK and Yvan!
use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
tell application "Finder"
set theItems to the selection
end tell
set tagList to choose from list {"Red", "Orange", "Yellow", "Green", "Blue", "Gray", "Purple"} with title "Tag Selected Items" with prompt "Choose colors:" OK button name "Apply" cancel button name "Cancel" with multiple selections allowed and empty selection allowed
repeat with thisItem in theItems
set theFile to POSIX path of (thisItem as alias)
(its setTags:tagList forPath:theFile)
end repeat
on returnTagsFor:posixPath -- get the tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags = missing value then return {} -- because when there are none, it returns missing value
return theTags as list
end returnTagsFor:
on setTags:tagList forPath:posixPath -- set the tags, replacing any existing
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:
on addTags:tagList forPath:posixPath -- add to existing tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
-- get existing tags
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags ≠ missing value then -- add new tags
set tagList to (theTags as list) & tagList
set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
end if
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTags:forPath: