Simplest smart rule to set a value from a custom metadata field that is a list of values?

I have a metadata field in my database defined as a set of values:

The value needs to be set based on what is written into the name of documents (specifically, in the names of files in an indexed folder of Zotero PDF files). Here is a sample of the files in the indexed folder:

I’m trying to write a smart rule that will set the metadata field value based on finding, e.g., “Book” in the name of the file. The problem I’m running into is that I can’t find a way to set the value of the metadata field using a value matched by a pattern. In other words, I seem to be stuck creating one rule for every possible value of the set, like this:

because the “Change Type” action of a smart rule needs to pick a specific value out of the set of values defined for the metadata field. This means I now have 15+ individual rules that each match a single substring of the file names and set the metadata field to one of the values out of the set of possible values.

Is there a more better, more maintainable way to do this? I could do regex matching in the names of the files, but then that doesn’t help selecting a value from the set of values because the “Change Type” action in smart rules can’t use the regex result. Is using a script in the smart rule action is the only way to do it, or have I missed a more clever solution?

(This is related to another question I asked about triggering these rules.)

An Execute Script action is the only option in this case.

Thanks for your shockingly fast reply! OK, in that case, can you point me to an example of how to set a metadata field value when the value type is “set” (as opposed to a text value, which I already know how to do).

You’re welcome

Something like this should work…

tell application id "DNtp"
	set sel to item 1 of (selected records)
	add custom meta data "black cherry" for "Test Set" to sel
end tell

However, @cgrunenberg: I am seeing an issue where a data definition with a multi-word name isn’t working as expected. It’s generating a new attribute instead. This is the result from the above snippet…

image

And I already have an established attribute named Test Set not being used…

image
This attribute existed long before I wrote the snippet.

OK, so there’s nothing special to do for values out of a set? It looks like it’s the same as setting a text string value.

Not unless @cgrunenberg has some quiet nugget of info about it I don’t know.

Ideally identifiers should be always used for scripting, not the displayed names.

Does that refer to metadata field names, or to value names? (And if value names, how do I find out what the identifiers are for the values? In the UI for the list of values in the set definition, I don’t see it displaying the corresponding identifiers for the values, but maybe I’m missing something.)

Each metadata definition has an internal identifier, see Preferences > Data. In case of values the desired (and shown) strings should of course work.

OK, just use the strings as the values; they’re not assigned unique identifiers. Got it. Thanks!

I tried that as well but I’ll retest in a bit.