Skip to content

Instantly share code, notes, and snippets.

@patrickleweryharris
Last active November 17, 2021 01:43
Show Gist options
  • Save patrickleweryharris/7e5f4afbba6f09199dd4677cc57dc70d to your computer and use it in GitHub Desktop.
Save patrickleweryharris/7e5f4afbba6f09199dd4677cc57dc70d to your computer and use it in GitHub Desktop.
Automatically save all email attachments in Apple Mail https://lewery.ca/automation/autosave-email-attachments
(*using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule*)
-- Adapted from: http://www.markosx.com/thecocoaquest/automatically-save-attachments-in-mail-app/
-- For usage instructions, see: https://lewery.ca/automation/autosave-email-attachments
set MainFolder to "Dropbox" -- After the current users home dir
set subFolder to "mail"
-- The folder to save the attachments in (must already exist)
tell application "Finder" to set attachmentsFolder to ((path to home folder as text) & MainFolder & ":" & subFolder) as text
tell application "Mail"
set theMessages to the selected messages of the front message viewer
repeat with eachMessage in theMessages
set theAddress to extract address from sender of eachMessage
if (count of (eachMessage's mail attachments)) > 0 then
try
tell application "Finder"
if not (exists folder theAddress of folder attachmentsFolder) then
make new folder at attachmentsFolder with properties {name:theAddress}
end if
end tell
-- Save the attachment
repeat with theAttachment in eachMessage's mail attachments
set originalName to name of theAttachment
set savePath to attachmentsFolder & ":" & theAddress & ":" & originalName
tell application "Finder"
if (exists file originalName of folder theAddress of folder attachmentsFolder) then
set savePath to attachmentsFolder & ":" & theAddress & ":" & originalName
end if
end tell
try
save theAttachment in file (savePath)
end try
end repeat
on error error_message number error_number
end try
end if
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