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 "<dict><key>CriterionUniqueId</key><string>$CRITERION_UNIQUE_ID</string><key>Expression</key><string>$EXPRESSION</string><key>Header</key><string>$HEADER</string></dict>" $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.
Criteria
array in the SyncedRules.plist. Doing it without opening the message would be ideal. Short of adding a menu item to the right-click menu, I wonder if I could use a flag and then any messages with x flag will be picked up.