Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RosemaryOrchard/e6d96eefd173f0991dd0128254e9d8d9 to your computer and use it in GitHub Desktop.
Save RosemaryOrchard/e6d96eefd173f0991dd0128254e9d8d9 to your computer and use it in GitHub Desktop.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
-- The folder to save the attachments in (must already exist)
set attachmentsFolder to ((path to home folder) & "Documents:Attachments") as string
-- Save in a sub-folder based on the name of the rule in Mail
set subFolder to name of theRule as string
tell application "Finder"
if not (exists folder subFolder of folder attachmentsFolder) then
make new folder at attachmentsFolder with properties {name:subFolder}
end if
end tell
tell application "Mail"
repeat with eachMessage in theMessages
-- Get the date the message was sent
set dateStamp to do shell script "date +%Y-%m-%d"
repeat with theAttachment in eachMessage's mail attachments
set originalName to name of theAttachment
set savePath to attachmentsFolder & ":" & subFolder & ":" & dateStamp & " " & originalName
-- try
save theAttachment in file (savePath) with replacing
-- end try
end repeat
-- delete message
delete eachMessage
end repeat
end tell
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