Skip to content

Instantly share code, notes, and snippets.

@mbreiden
Last active August 29, 2015 14:05
Show Gist options
  • Save mbreiden/4a2cebf6989f190fcd47 to your computer and use it in GitHub Desktop.
Save mbreiden/4a2cebf6989f190fcd47 to your computer and use it in GitHub Desktop.
An Applescript to implement some Sanebox http://sanebox.com/t/m66z6 functionality in Mail
-- some parts taken from http://bit.ly/SaneBoxSpamSieve
-- original post on http://wp.me/p4WzrV-12
on accountAndMailboxNames()
-- Enter your account name, followed by a list of mailboxes for that account that should be filtered.
-- The account name comes from the "Description" field in the Accounts tab of Mail's preferences.
-- For example, this would filter the @SaneLater mailbox of Account 1:
-- return {{"Account 1", {"@SaneLater"}}}
-- And this would filter the @SaneLater and Other mailboxes of the Personal account
-- and the @SaneLater mailbox of the Work account:
-- return {{"Personal", {"@SaneLater", "Other"}}, {"Work", {"@SaneLater"}}}
return {{"Account 1", {"@SaneLater"}}}
end accountAndMailboxNames
on run
-- This is executed when you run the script directly.
my filterSaneMailboxes()
end run
using terms from application "Mail"
on perform mail action with messages _messages
-- This is executed when Mail runs the rule.
my filterSaneMailboxes()
end perform mail action with messages
end using terms from
on filterSaneMailboxes()
my logToConsole("Running Poor Man's Sanebox")
tell application "Mail"
repeat with _pair in my accountAndMailboxNames()
set {_accountName, _mailboxNames} to _pair
try
set _account to account _accountName
repeat with _mailboxName in _mailboxNames
my filterAccountMailboxName(_account, _mailboxName)
end repeat
on error _error
my logToConsole("Error checking mailbox “" & _mailboxName & "” from account “" & _accountName & "”: " & _error)
end try
end repeat
end tell
my logToConsole("Poor Man's Sanebox is done")
end filterSaneMailboxes
on filterAccountMailboxName(_account, _mailboxName)
set staledate to (current date) - (60 * days)
if _mailboxName contains "Tomorrow" then
set staledate to (current date) - (1 * days)
end if
if _mailboxName contains "NextWeek" then
set staledate to (current date) - (7 * days)
end if
tell application "Mail"
set _mailbox to _account's mailbox _mailboxName
try
set _inbox to _account's mailbox "Inbox"
on error _error
set _inbox to _account's mailbox "INBOX"
end try
with timeout of 60 seconds
set _messages to messages of _mailbox whose read status is false
end timeout
repeat with _message in _messages
set read status of _message to true
end repeat
with timeout of 60 seconds
set _messages to messages of _mailbox whose date received is less than staledate
end timeout
repeat with _message in _messages
set read status of _message to false
move _message to _inbox
end repeat
end tell
end filterAccountMailboxName
on logToConsole(_message)
set _logMessage to "[Poor Man's Apple Mail SaneBox] " & _message
do shell script "/usr/bin/logger -s " & _logMessage's quoted form
end logToConsole
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment