2

I want to edit manually the following .plist file:

/Users/<myname>/Library/Mail/V5/MailData/SyncedRules.plist

Because I get a lot of spam and adding each spammer address to the rules using Mail is impraticable:

enter image description here

and there are many addres to be added. Since I know a bit bash scripting, sed and awk I'm thinking to write a script that automatically adds the addresses from selected email to the plist file above.

enter image description here


So, what is my question?

In conclusion, for each spammer addres the script should add a new dict entry with following fields:

        <dict>
            <key>CriterionUniqueId</key>
            <string>CC4CB669-0D44-4A32-80B1-02D069718304</string> (*)
            <key>Expression</key>
            <string>spammer@address.spam</string>
            <key>Header</key>
            <string>From</string>
        </dict>

How the string at (*) is generated? How can I fill it?

  • 2
    You could invest in something like SpamSieve which will handle it all for you. No affiliation, I've just been using it for so long I forget it's there ;) – Tetsujin Jan 26 '18 at 7:40
  • I appreciate your comment ☺️ but SpamSieve costs 30$ 🤨 Why buy it when I could do it from myself? 🤗 – kitsune Jan 26 '18 at 16:47
1

Maybe you will appreciate this script. Basically the code will get all of the email addresses from the senders of all emails located in your junk mailbox and will make a new mail rule for each..;. Setting the name of the rule to the extracted email address. In the event there are duplicate items in the senders list, all duplicates will be removed before adding the rules. Additionally, a dialog box will appear giving you the option to enable the newly created rules. This code should prove to be a huge time saver.

This works for me using the latest version of macOS High Sierra

set isRunning to application "Mail" is running

tell application "Mail"
    activate
    if isRunning is false then
        delay 12
    else
        delay 2
    end if
    set junkEmailAddressesNoDupes to {}
    set junkEmailAddresses to sender of every message of junk mailbox
    if (count of junkEmailAddresses) is 0 then
        set zeroJunkMail to button returned of (display dialog "You Currently Have 0 Junk Mail Messages" buttons {"OK"} with icon 1 default button "OK" giving up after 10)
        if zeroJunkMail is "OK" then
            if isRunning is false then
                tell application "Mail" to quit
            end if
            return
        end if
        if isRunning is false then
            tell application "Mail" to quit
        end if
        return
    end if
    activate
    display alert ¬
        "IMPORTANT" message "Please Make Sure The Top Level Junk Mail Folder labeled" & " " & quote & "JUNK" & quote & ¬
        " Is Selected" as warning ¬
        buttons {"Cancel", "OK"} ¬
        default button 2 ¬
        cancel button 1 ¬
        giving up after 30
    delay 10
    repeat with i from 1 to count of junkEmailAddresses
        set thisItem to item i of junkEmailAddresses
        set thisEmail to extract address from thisItem
        set end of junkEmailAddressesNoDupes to thisEmail
    end repeat
end tell

set junkEmailAddressesNoDupes2 to removeDuplicates(junkEmailAddressesNoDupes)

tell application "Mail"
    activate
    set checkRulez to button returned of (display dialog ¬
        "Please Make Sure These New Junk Mail Rules Do Not Contain Any Valid Email Addresses That You Don't Want Tagged As Junk" & linefeed & linefeed & (junkEmailAddressesNoDupes2 as list) buttons {"CANCEL", "OK"} ¬
        default button 2 ¬
        cancel button 1 ¬
        with title ¬
        "NEW JUNK MAIL RULES" with icon 1 ¬
        giving up after 120)
end tell

if checkRulez is "CANCEL" then
    return
end if

repeat with i from 1 to count of junkEmailAddressesNoDupes2
    set thisItem to item i of junkEmailAddressesNoDupes2
    tell application "Mail"
        if not (exists of rule thisItem) then
            make new rule ¬
                with properties {name:thisItem}
            tell its rule thisItem
                make new rule condition ¬
                    with properties {header:"", expression:thisItem, rule type:from header, qualifier:equal to value}
                delay 0.1
                set delete message to true
            end tell
        end if
    end tell
end repeat

tell application "Mail"
    activate
    set enableRulez to button returned of (display dialog ¬
        "Enable New Rules?" buttons {"Cancel", "No", "Yes"} ¬
        default button 3 ¬
        cancel button 1 ¬
        with title ¬
        "New Rulez" giving up after 30)
end tell

if enableRulez is "Yes" then
    tell application "Mail" to set enabled of every rule to true
else
    return
end if

tell application "Mail"
    activate
    set applyRulez to button returned of (display dialog ¬
        "Would You Like To Apply Your New Junk Mail Rules To Your Current Junk Mail?" buttons {"No", "Yes"} ¬
        default button 2 ¬
        cancel button 1 ¬
        with title ¬
        "APPLY THE NEW RULES?" with icon 1 ¬
        giving up after 30)
end tell

if applyRulez is "Yes" then
    tell application "System Events"
        tell application "Mail" to activate
        click static text 1 of UI element 1 of row 5 of outline 1 of scroll area 1 ¬
            of splitter group 1 of window 1 of application process "Mail"
        delay 1
        keystroke "a" using command down
        delay 0.5
        keystroke "l" using {option down, command down}
    end tell
else
    return
end if


on removeDuplicates(lst)
    try
        if lst's class is not list then error "not a list." number -1704
        script k
            property l : lst
            property res : {}
        end script
        repeat with itemRef in k's l
            set itm to itemRef's contents
            if k's res does not contain {itm} then ¬
                set k's res's beginning to itm
        end repeat
        return k's res's reverse
    on error eMsg number eNum
        error "Can't removeDuplicates: " & eMsg number eNum
    end try
end removeDuplicates

enter image description here

enter image description here

  • If I am reading this correctly it's getting all the email addresses from junk mail and deleting all email with those email addresses. Why not just delete all emails in junk mail? Mail will automatically move emails marked as junk into junk mail folder. – JBis Jul 31 '18 at 10:40
  • @JBis No you are not reading that correctly. The code will take all of the email addresses from the senders in your junk mailbox, and create a new junk mail rule for each one of the senders. The user will still need to activate the newly created rules. This is a failsafe just in case some legitimate emails ended up in the junk box. For those messages to be deleted, the user still needs to activate the rules, then select the junk emails in the junkbox and “Apply Rules” – wch1zpink Aug 1 '18 at 16:03
  • But the rule is set to delete them, correct? So the point of the script is allow you to do delete Junk mail based on email address. – JBis Aug 1 '18 at 16:07
  • That is just the rule action I set, Feel free to adjust the code To have it perform whatever action you choose – wch1zpink Aug 1 '18 at 16:08
  • Ok. I understand the idea behind it now. – JBis Aug 1 '18 at 16:09

You must log in to answer this question.

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