[SOLVED] Apple Mail – Add Email Address to Rules

I have an Apple Mail rule titled 'Delete' which automatically deletes email I do not wish to receive which email does not allow for unsubscribing. I want to use KM to add the current on screen email to that list and select 'Apply'. I am having difficulity selecting the Delete rule for editing. The keystrokes required are Mail >Preferernces >Rules >Delete >Edit >(select topmost circled +) >OK >Apply. I can get to Mail > Preferences >Rules, but I cannot seem to get the specific rule (Delete) to select so that It can be edited. Please help...

Thanx

I think rules in Mail can be created/edited with AppleScript.

Which element(s) of the current on-screen mail do you want to add as condition?
(Sender? Sender domain? Subject? …)

Here an example:

This will add the sender(s) of the selected message(s) as new condition(s) to your existing “Delete” rule:

use AppleScript version "2.4" -- Yosemite (10.10) or later

set theProperties to {}

tell application id "com.apple.mail" -- Mail.app
  set theMessages to the selection
  repeat with i in theMessages
    set the end of theProperties to sender of i
  end repeat
  tell rule "Delete"
    repeat with i in theProperties
      make rule condition at end of rule conditions with properties {rule type:from header, qualifier:does contain value, expression:i}
    end repeat
  end tell
end tell

[demo] Add Conditions to Existing Mail Rule “Delete”.kmmacros (2.5 KB)

  1. Download and import the macro from the above link.
  2. Make sure the macro and the macro group are activated.
  3. In Mail select one or more messages.
  4. Run the macro (default trigger: ⌃⌥⌘F1).
  5. Check your “Delete” rule: the senders of the selected messages should have been added there as new conditions.
1 Like

Sender

If Sender, then the demo macro from above should fit. Give it a try, we can always modify it.

Sorry Tom… Guess I just don’t understand. I have followed your instructions. Download >activated >select email message >enter the trigger… at this point all I get is a single 'ding' (bell). I just don’t get it. The macro is there in KM and it is activated… but it just rings. I have tried several times and ways. I always just get a bell… a single 'ping'. You have any more ideas?

Oops… OK I found the reason for the ding… Already had that trigger in use.

Regardless, I changed the trigger and the script does not add the sender to the Delete rule… Please know that when I add manually there is a maybe a five second delay after clicking 'Edit' because my delete list is rather long. Could that be a problem?

No. I just tested this:

  1. With the script I added ~400 senders to my “Delete” rule
    • Now opening the rule in Apple Mail for editing takes about 10 seconds
  2. With the script I added 3 more senders
    • Script worked fine and fast, the 3 senders have been added at the end of the existing 400 senders

Try to enable the script output to a window, and see if there are any errors or hints:


The output you should get, if the script works fine, is something like this:

29-pty-fs8

(This is the index of the last condition added.)

OK, got it… The error was "Not authorized to send apple events to mail." I Googled that and found a couple fixes. The second fix was to enter "tccutil reset AppleEvents; tccutil reset SystemPolicyAllFiles" in Terminal. It now works.

Thanx for your assistance… greatly appreciated!

Ken

Thanks for your feedback. Happy that it works now.

If one of my posts served as a solution or major help to your issue/question, feel free to set the “solved” check mark at the bottom of that post.

And, of course, don’t hesitate to post any further questions on the topic, e.g. how to modify the AppleScript or whatever.

I'm trying to rebuild a macro so it will add only the major domain to my delete mail rules and I am having a bit of trouble.

Debugging reflects that everything works until the AppleScript, so I need some different eyes to look at my macro.

Here is the macro:

Delete Mail Rule.kmmacros (10.6 KB)

And here is the macro image:

image
image

You're pulling in the variable incorrectly. Try:

set DeleteThis to getvariable "theMajorDomain"

getvariable pulls the KM variable into AppleScript, setvariable pushes your AppleScript value into your KM variable.

Yep... Just figured that out about two minutes ago! Thanx much!

My new Apple script is:

set DeleteThis to ""
tell application "Keyboard Maestro Engine"
   set DeleteThis to getvariable "theMajorDomain"
end tell
tell application id "com.apple.mail" -- Mail.app
   tell rule "Delete"
      make new rule condition at end of rule conditions with properties {rule type:from header, qualifier:does contain value, expression:DeleteThis}
   end tell
end tell

I finally have this macro working very well... Almost. Please take a look at it and advise. What I would like is to have the macro check to see if indeed some mail messages have been selected so I could remove the Prompt for User Input action. How can I have Keyboard Maestro perform such a check? Alternatively, is there a way to add such a check to the Applescript?

Keyboard Maestro Actions.kmactions (2.8 KB)

Why?

Your AppleScript works on each item of a list. That list is a list of selected items. If no item is selected the list is empty, there's nothing to process, and the script skips the whole repeat block then exits without doing anything.

So there's nothing "wrong" with running the macro with nothing selected -- it won't throw an error or do something weird. What you might want to do is inform the user that it's a pointless exercise :wink: You may also want to catch the fact to use it later in your macro, perhaps putting the message dialog up there instead of with your AppleScript.

For an AppleScript prompt try changing the beginning off the script to

tell application "Mail"
    set theMessages to the selection
-- add this bit from here...
    if theMessages is {} then
        display alert "Nothing was selected!" message "You must select one or more messages in Mail" as critical
        return "Empty selection"
    end if
-- ...to here
    repeat with theMessage...

...or if you'd like to handle the error with a KM dialog (and maybe some further processing) change the action's "results" option to "Save results in a variable"

tell application "Mail"
    set theMessages to the selection
-- add this bit from here...
    if theSelection is {} then return "Empty selection"
-- ...to here
    repeat with theMessage...
    -- lots more script
    end repeat
    -- and add this bit from here...
    return "Selection Processed
    -- ...to here
end tell

...and you can then test the variable you've used in a later KM action to see if it contains "Empty selection" or "Selection processed".

Way kewl... I like! Thanx Nige_S!!!