How can I automate adding sender addresses to a Rule in macOS Mail? So I've created a Mail Rule "Bin Spam Messages" that will delete messages from a sender. What I can't figure out is how to automate adding new sender addresses to the rule. I'm guessing I could do this with AppleScript but I'm also trying to do it without opening the message. The goal is to automatically delete spam messages from my inbox and junk mail folders. I thought this was what the Apply Rules menu option was for but apparently not.
Any ideas?

Update: I have tried updating SyncedRules.plist using a Shell script (below) but (a) it doesn't show up in Mail preferences; and (b) SyncedRules is restored after relaunching Mail. Not sure where else Mail rules are stored.

#! /bin/zsh

# Generate 128-bit UUID
CRITERION_UNIQUE_ID="$(uuidgen)"

# Define Shell input variables
EXPRESSION="$1"
HEADER="$2"

# Define SyncedRules.plist file
FILENAME="/Users/tim/Library/Mail/V7/MailData/SyncedRules.plist"

# Run plutil
plutil -insert 0.Criteria.0 -xml "&lt;dict&gt;&lt;key&gt;CriterionUniqueId&lt;/key&gt;&lt;string&gt;$CRITERION_UNIQUE_ID&lt;/string&gt;&lt;key&gt;Expression&lt;/key&gt;&lt;string&gt;$EXPRESSION&lt;/string&gt;&lt;key&gt;Header&lt;/key&gt;&lt;string&gt;$HEADER&lt;/string&gt;&lt;/dict&gt;" $FILENAME


This script inserts a dictionary into the first items Criteria array containing a 128-bit UUID, sender email address and email header, but my issue is persistence. If I run the command with sudo, I can update the SyncedRules.plist but Mail doesn't recognise the changes and the .plist is restored when Mail relaunches, anyway.

Getting warmer...

More Updates: So I doubled back and had a look at the Mail library in Script Editor.. then discovered this answer https://apple.stackexchange.com/a/332482/34929 - there's a block there that I'm particularly interested in... something like make new rule with properties {header:"", expression:thisItem, rule type:from header, qualifier:equal to value} condition - I think this is where I'm heading now.
Mail remembers tabs in Preferences window, and the workflow is highly simplified using "edit rule" to add new senders and to add new actions or use the already specified ones for them. You'd anyway have to add a new sender's mail ID manually. For these reasons, I am not very keen over having a script or automator action. 

However, I decided to put my comments into a partial answer.


https://stackoverflow.com/questions/3542826/applescript-editor-record-doesnt-work/18752770#18752770


This answer documents a way to use Automator to record the actions you perform in the mail app. Here's the workflow. Select all steps and copy paste in "Script Editor App" for getting the code. https://pastebin.com/RNSShvWK



Here are the details from Preferences Window of Mail app using "Accessibility Inspector". Hierarchy pane seems useful. The button that looks like a gun's aim can be used to inspect different applications.



You are encouraged to modify the script/workflow to your use and update the question if you get stuck.