Skip to content

Instantly share code, notes, and snippets.

@jayv
Last active February 2, 2019 21:18
Show Gist options
  • Save jayv/1d4d763f24527fb47ab8399820a73395 to your computer and use it in GitHub Desktop.
Save jayv/1d4d763f24527fb47ab8399820a73395 to your computer and use it in GitHub Desktop.
AppleScript for use in an Automator Service to bind to a shortcut to mute threads in Mail.app (like gmail mute)
on run {input, parameters}
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "Mail" is in activeApp then
tell application "Mail"
-- Collect the rules that already exist
set ruleNames to {}
repeat with ruleNumber from 1 to count rules
copy name of item ruleNumber of rules to end of ruleNames
end repeat
-- Find the threads we want to mute
set addedRules to 0
repeat with messageNumber from 1 to count selection
set read status of item messageNumber of (selection as list) to true
set theSubject to the subject of item messageNumber of (selection as list)
ignoring case
if theSubject does not start with "Re: " then set theSubject to "Re: " & theSubject
end ignoring
set newRuleName to "Mute thread: " & theSubject
delete item messageNumber of (selection as list)
-- Add the rule if it doesn't exist already
if newRuleName is not in ruleNames then
copy newRuleName to end of ruleNames
set newRule to make new rule with properties {name:newRuleName, delete message:true, enabled:true}
make new rule condition at end of rule conditions of newRule with properties {qualifier:ends with value, rule type:subject header, expression:theSubject}
set enabled of newRule to true -- for some reason, without this the new rules are disabled
set addedRules to addedRules + 1
end if
end repeat
set result to "Mail.app - MUTED +" & addedRules & " rules."
display notification result
get "Added " & addedRules & " rules."
end tell
else
display notification "Mail.app not in foreground - ignore MUTE"
end if
end tell
return input
end run
@dabrahams
Copy link

@jayv this is cool but I'd be afraid to use it based on subject matching, because unrelated threads can easily have the same subject line. The right way to tell if a message is part of the same thread is to look in the References: header of the message. I'm not sure if that's accessible via applescript, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment