-
-
Save shawnhank/db0603d2ad6b2ff8f96f4aa783388fbd to your computer and use it in GitHub Desktop.
Filter emails in Mail.app with regular expressions
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
-- Mail.app can't run a "Contains" filter with a regex, | |
-- so you can't filter on HTML content. Until now. | |
using terms from application "Mail" | |
on perform mail action with messages theMessages for rule theRule | |
try | |
repeat with theMessage in theMessages | |
-- Getting the content as string converts all HTML tags to '?' and just leaves text content | |
set theBody to quoted form of (theMessage's content as string) | |
-- It's awkwardly hard to get sed to work w/ mult. lines, so collapse newlines | |
set theCommandString to "echo " & theBody & " | tr '\\n' ' ' | sed \"s/brett Author/*MATCHED*(&)/\"" as string | |
set sedResult to do shell script theCommandString | |
if sedResult contains "*MATCHED*" then | |
tell application "Mail" | |
move the theMessage to mailbox "My special mailbox" | |
end tell | |
end if | |
end repeat | |
on error errorText number errorNumber | |
tell application "Mail" to display alert ("Error: " & errorNumber) message errorText | |
return | |
end try | |
end perform mail action with messages | |
end using terms from |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment