I wanted to automatically save attachments in Mail to my hard drive. After lots of research I set a rule in Mail that runs the script below whenever an email with an attachment arrives.
use scripting additions
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
set destinationPath to (POSIX file "/volumes/Data/Dropbox/WORK ITEMS/Email Attachments/") as string
tell application "Mail"
repeat with aMessage in messageList
repeat with anAttachment in mail attachments of aMessage
set senderName to (extract name from sender of aMessage)
set {year:y, month:m, day:d, hours:h, minutes:min} to date received of aMessage
set timeStamp to (d & "/" & (m as integer) & "/" & y) as string
set attachmentName to senderName & " - " & timeStamp & " - " & name of anAttachment
set doSave to true
set originalName to name of anAttachment
if originalName contains "jpg" then
set doSave to false
else if originalName contains "jpeg" then
set doSave to false
else if originalName contains "gif" then
set doSave to false
else if originalName contains "png" then
set doSave to false
else if originalName contains "html" then
set doSave to false
end if
if doSave is true then save anAttachment in file (destinationPath & attachmentName)
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
This works fine. The exclusions stop the script saving logos in email signatures etc. But this script does mean all the files are in one directory (Email Attachments).
As the script has the name of the sender what I have been trying to do is create a subdirectory in 'Email Attachments' using the sender name (bur only creating it if it doesn't exist) and then save the file as 'timestamp & name' of the attachment in that directory.
This way I will have a directory structure under 'Email Attachments' of all the sender names with their attachments in their own directory.
I have tried everything I can - and haven't succeeded (I can post my latest attempt here but would rather save my blushes!!)
Does anyone know how to amend my script to make this work??? Thank you if you have any ideas...
To test if the target folder exists, and if not create it, you can use the following example AppleScript code:
tell application "System Events"
if not (exists folder (destinationPath & senderName)) then
make new folder at end of alias destinationPath with properties {name:senderName}
end if
end tell
Another way to create the folder if it doesn't exist is:
do shell script "mkdir -p " & (POSIX path of destinationPath & senderName)'s quoted form
This will create it if it doesn't exist, and if it does it does nothing.
On a side note, here is a way to eliminate the if
... else if
block of code you have:
set doSave to true
set originalName to name of anAttachment
set doNotSaveFileTypeList to {"jpg", "jpeg", "gif", "png", "html"}
set AppleScript's text item delimiters to "."
set fileExtension to last text item of originalName
set AppleScript's text item delimiters to ""
if the doNotSaveFileTypeList contains fileExtension then
set doSave to false
end if
When the action is the same for all the list items, I like using this method as it's easier to add or remove from the list without having to modify the rest of the code block.
Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.