Skip to content

Instantly share code, notes, and snippets.

@logic2design
Last active December 8, 2023 22:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save logic2design/15a677ed42a11633eabebb592dc5870e to your computer and use it in GitHub Desktop.
Save logic2design/15a677ed42a11633eabebb592dc5870e to your computer and use it in GitHub Desktop.
This will add either the Sender's email address or the Sender's Domain to a Mail rule.
#################################################################################
# Title: Mail - Update Rule
#################################################################################
#Iain Dunn
#Logic2design.com
#logic2design@icloud.com
# Contributors, inspration and sources
# https://leancrew.com/all-this/2021/06/hey-i-sped-up-apple-mail-rules/
set defaultRule to "πŸŒ‘ The Black Hole - Sender"
(choose from list {"πŸŒ‘ The Black Hole - Sender", "πŸŒ‘ The Black Hole - Domain", "πŸ“Š Business - Sender", "πŸ“Š Business - Domain", "πŸ’² Finance - Sender", "πŸ’² Finance - Domain", "πŸ“– Recreation - Sender", "πŸ“– Recreation - Domain", "πŸ‘±β€β™‚οΈ Personal - Sender", "πŸ‘±β€β™‚οΈ Personal - Domain", "πŸ›’ Purchases - Sender", "πŸ›’ Purchases - Domain", "πŸ›  Work - Sender", "πŸ›  Work - Domain"} default items defaultRule OK button name "Select" with prompt "Select the Rule" with title "Add to Mail Rule")
set theRule to result as text
if the theRule = "πŸŒ‘ The Black Hole - Sender" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸŒ‘ The Black Hole"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
set qualifier to equal to value
set expression to spamAddr
end tell
end tell
-- Delete the message
set mailbox of item loop of theMessages to mailbox "Trash" of acct
-- Next Message
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸŒ‘ The Black Hole - Domain" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
set normDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "@"
set theDomain to text item 2 of spamAddr
set AppleScript's text item delimiters to normDelims
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸŒ‘ The Black Hole"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
-- set qualifier to equal to value
set qualifier to does contain value
set expression to theDomain
end tell
end tell
-- Delete the message
set mailbox of item loop of theMessages to mailbox "Trash" of acct
set loop to loop + 1
end repeat
end if
end tell
else if the theRule = "πŸ’² Finance - Sender" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ’² Finance"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
set qualifier to equal to value
set expression to spamAddr
end tell
end tell
-- Delete the message
set mailbox of item loop of theMessages to mailbox "Trash" of acct
-- Next Message
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ’² Finance - Domain" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
set normDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "@"
set theDomain to text item 2 of spamAddr
set AppleScript's text item delimiters to normDelims
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ’² Finance"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
-- set qualifier to equal to value
set qualifier to does contain value
set expression to theDomain
end tell
end tell
-- Delete the message
set mailbox of item loop of theMessages to mailbox "Trash" of acct
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ‘±β€β™‚οΈ Personal - Sender" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ‘±β€β™‚οΈ Personal"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
set qualifier to equal to value
set expression to spamAddr
end tell
end tell
-- Next Message
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ‘±β€β™‚οΈ Personal - Domain" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
set normDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "@"
set theDomain to text item 2 of spamAddr
set AppleScript's text item delimiters to normDelims
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ‘±β€β™‚οΈ Personal"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
-- set qualifier to equal to value
set qualifier to does contain value
set expression to theDomain
end tell
end tell
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ›’ Purchases - Sender" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ›’ Purchases"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
set qualifier to equal to value
set expression to spamAddr
end tell
end tell
-- Next Message
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ›’ Purchases - Domain" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
set normDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "@"
set theDomain to text item 2 of spamAddr
set AppleScript's text item delimiters to normDelims
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ›’ Purchases"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
-- set qualifier to equal to value
set qualifier to does contain value
set expression to theDomain
end tell
end tell
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ“– Recreation - Sender" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ“– Recreation"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
set qualifier to equal to value
set expression to spamAddr
end tell
end tell
-- Next Message
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ“– Recreation - Domain" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
set normDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "@"
set theDomain to text item 2 of spamAddr
set AppleScript's text item delimiters to normDelims
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ“– Recreation"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
-- set qualifier to equal to value
set qualifier to does contain value
set expression to theDomain
end tell
end tell
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ›  Work - Sender" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ›  Work"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
set qualifier to equal to value
set expression to spamAddr
end tell
end tell
-- Next Message
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ›  Work - Domain" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
set normDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "@"
set theDomain to text item 2 of spamAddr
set AppleScript's text item delimiters to normDelims
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ›  Work"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
-- set qualifier to equal to value
set qualifier to does contain value
set expression to theDomain
end tell
end tell
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ“Š Business - Sender" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ“Š Business"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
set qualifier to equal to value
set expression to spamAddr
end tell
end tell
-- Next Message
set loop to loop + 1
end repeat
end if
end tell
else if theRule = "πŸ“Š Business - Domain" then
tell application "Mail"
-- Start by getting the sender's address and the message's account
set theMessages to selection
--repeat with a in (theMessages)
if theMessages is not {} then -- check empty list
set loop to 1
repeat with i from 1 to the number of items in theMessages
set acct to account of mailbox of item loop of theMessages
set spamAddr to extract address from sender of item loop of theMessages
set normDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "@"
set theDomain to text item 2 of spamAddr
set AppleScript's text item delimiters to normDelims
get acct
-- Add that address to a new condition of the rule
set psRule to rule "πŸ“Š Business"
tell psRule
set newCondition to make new rule condition at beginning of rule conditions
tell newCondition
set rule type to from header
-- set qualifier to equal to value
set qualifier to does contain value
set expression to theDomain
end tell
end tell
set loop to loop + 1
end repeat
end if
end tell
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment