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...
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
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?
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.
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?
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 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".