Last active
December 8, 2023 22:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################# | |
# Title: Mail - Update Rule | |
################################################################################# | |
#Iain Dunn | |
#Logic2design.com | |
#[email protected] | |
# 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