0

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.

5
  • apple.stackexchange.com/a/378085/313842
    – anki
    Commented Dec 24, 2019 at 13:57
  • I'm not seeing this as a duplicate question about how to block a sender using rules, @ankii. I read this as the OP has already created the rule and now they want to automate adding additional addresses to the existing rule.
    – fsb
    Commented Dec 24, 2019 at 14:36
  • @fsb adding a rule doesn't look like a task to be automated. I don't see reduction in clicks, and one would anyway have to type in the sender's email address, whether in script or in rules. Also, since one rule can have multiple conditions and multiple actions, it is simplified already.
    – anki
    Commented Dec 24, 2019 at 15:02
  • 1
    @ankii agree, so I think that's actually the answer to this question. It looks like a different question to the one you linked to, that was my point.
    – fsb
    Commented Dec 24, 2019 at 15:04
  • I updated my question with more information. I think the best way will be to add criterion to the 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.
    – TimD
    Commented Dec 25, 2019 at 2:09

1 Answer 1

0

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.

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

Workflow

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.

Accessibility Inspector

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

5
  • I updated my question with a Shell script that updates, or is meant to update, the SyncedRules.plist... but it doesn't stick.
    – TimD
    Commented Dec 26, 2019 at 4:44
  • I don't think that you've added the "actions" to take. Make a rule yourself, to say flag something blue, and see what changes in the file and use that as the argument of plutil. i.sstatic.net/lCcHw.png: This is the complete rule. You can even use Plistpro' free trial to visualise the structure, and easy copy pasting.
    – anki
    Commented Dec 26, 2019 at 5:03
  • Sorry, I wasn't using Automator. I was just trying to see if I could update the .plist from Terminal, first.
    – TimD
    Commented Dec 26, 2019 at 5:31
  • yes, use this GUI tool to do so first and then keep that "new" dict in a note. Then try with Terminal. Remember to keep a backup of that file.
    – anki
    Commented Dec 26, 2019 at 5:38
  • I updated my question with some more infos.. actually found a similar question with an anser that pretty much does what I'm looking for.
    – TimD
    Commented Dec 26, 2019 at 6:40

Not the answer you're looking for? Browse other questions tagged .