Skip to content

Instantly share code, notes, and snippets.

@karlcow
Last active December 19, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlcow/6011345 to your computer and use it in GitHub Desktop.
Save karlcow/6011345 to your computer and use it in GitHub Desktop.
For a few years, I have been filtering my email with a procmail rule on my own server. Every mail arriving on July 2013, is getting filed in /2013/07/ and so on. For people who have no access to servers and procmail, it sucks. So at least for Mail.app I have created an applescript filtering email automatically in the right folder. You just need …
using terms from application "Mail"
on perform mail action with messages theMessages
tell application "Mail"
-- FIX HERE TO YOUR OWN MAIL ACCOUNT
set myMailAccount to "moz"
repeat with eachMessage in theMessages
set messageDate to eachMessage's date sent
-- we get the year ex: 2013
set y to year of messageDate
-- convert the year as a string ex: "2013"
set y to y as string
-- we get the month ex: 7
set m to month of messageDate as integer
-- we convert the month from integer to string ex: "7"
set m to m as string
-- if the month is one character we add a leading "0" ex: "07"
if the length of m is 1 then
set m to "0" & m
end if
tell account myMailAccount
set datedMailbox to y & "/" & m
if not (exists mailbox datedMailbox) then
my logit("MAILDATEFILTER: creating mailbox" & datedMailbox, "applescript")
set mbox to make new mailbox with properties {name:y & "/" & m}
end if
-- we move the message from Inbox to dated space
tell application "Mail"
move eachMessage to mailbox datedMailbox of account myMailAccount
my logit("MAILDATEFILTER: message moved to " & datedMailbox & " of " & myMailAccount, "applescript")
end tell
end tell
end repeat
end tell
end perform mail action with messages
end using terms from
to logit(log_string, log_file)
do shell script ¬
"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
"\" >> $HOME/" & log_file & ".log"
end logit
2013-07-16 14:28:14: MAILDATEFILTER: message moved to 2013/07 of FooBar
2013-07-16 14:28:14: MAILDATEFILTER: message moved to 2013/07 of FooBar
2013-07-16 14:28:15: MAILDATEFILTER: message moved to 2013/07 of FooBar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment