Hi All,
Im looking for a way to automatically have attachments from certain emails automatically saved to a specific folder. Figuring there might be a way to utilize KM and maybe AppleScript but not sure if thats needed.
Any thoughts and suggestions deeply appreciated!
Thanks!
In Apple Mail, set up an inbox rule for the From header corresponding to the sender you're interested in, and have it run the AppleScript below:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
set attachmentsFolder to ((path to downloads folder) as Unicode text) & "Attachments"
tell application "Mail"
repeat with eachMessage in theMessages
--set the sub folder for the attachments to the senders mail address.
-- All future attachments from this sender will the be put here.
set subFolder to (sender of eachMessage)
-- set up the folder name for this mail message's attachments. We use the time stamp of the date received time stamp
set {year:y, month:m, day:d, hours:h, minutes:min} to eachMessage's date received
set timeStamp to (y & "-" & my pad(m as integer) & "-" & my pad(d) & "_" & my pad(h) & "-" & my pad(min)) as string -- month as number
set attachCount to count of (mail attachments of eachMessage)
if attachCount is not equal to 0 then
display notification ¬
"Mail is saving attachments!" with title ¬
"Saving Attachments" subtitle "Note any system unresponsiveness"
-- use the unix /bin/test command to test if the timeStamp folder exists. if not then create it and any intermediate directories as required
if (do shell script "/bin/test -e " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder & "/" & timeStamp) & " ; echo $?") is "1" then
-- 1 is false
do shell script "/bin/mkdir -p " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder & "/" & timeStamp)
end if
try
-- Save the attachment
tell eachMessage
repeat with theAttachment in (mail attachments)
set originalName to name of theAttachment
set savePath to attachmentsFolder & ":" & subFolder & ":" & timeStamp & ":" & originalName
try
with timeout of 1800 seconds
save theAttachment in file (savePath)
end timeout
end try
end repeat
end tell
on error msg
display notification msg
end try
end if
end repeat
end tell
end perform mail action with messages
end using terms from
on pad(n)
return text -2 thru -1 of ("0" & n)
end pad
I'll Take Your advice. Thanks!
Thanks again for your help. I'm relatively new tonAppleScript but think I understand the gist. I don’t need the subfolder aspect, I simply need the attachments from certain people dropped into a folder where I have a folder action setup.
Would you mind telling me how to alter this script to just save the attachments into a folder? I'm not sure what part to delete. Many Thanks!
Marc
The script below should do the trick then (not tested). You can edit the line that starts "set attachmentsFolder" to point to any folder you want, but be sure to include the trailing colon.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
set attachmentsFolder to ((path to downloads folder) as Unicode text) & "Attachments:"
tell application "Mail"
repeat with eachMessage in theMessages
set attachCount to count of (mail attachments of eachMessage)
if attachCount is not equal to 0 then
try
-- Save the attachment
tell eachMessage
repeat with theAttachment in (mail attachments)
set savePath to attachmentsFolder & (name of theAttachment)
try
with timeout of 1800 seconds
save theAttachment in file (savePath)
end timeout
end try
end repeat
end tell
on error msg
display notification msg
end try
end if
end repeat
end tell
end perform mail action with messages
end using terms from
Thank you so much for your help. Ill try testing this.
I likely am doing something incorrectly, but this didn’t work. I set the rule in email to run this script. I manually ran the rules and nothing happened. Here is what I have. Any Thoughts?
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
set attachmentsFolder to (":Volumes:Macintosh 2nd HD:Marc's 2nd HDD:marcpearlman 2nd HDD:Dropbox:PDF File Inbox:" as Unicode text) & "Attachments"
tell application "Mail"
repeat with eachMessage in theMessages
set attachCount to count of (mail attachments of eachMessage)
if attachCount is not equal to 0 then
try
-- Save the attachment
tell eachMessage
repeat with theAttachment in (mail attachments)
set savePath to attachmentsFolder & (name of theAttachment)
try
with timeout of 1800 seconds
save theAttachment in file (savePath)
end timeout
end try
end repeat
end tell
on error msg
display notification msg
end try
end if
end repeat
end tell
end perform mail action with messages
end using terms from
There seems to be something wrong with your attachmentsFolder path — it looks like you have multiple nested hard drives, which doesn't make sense (unless they're just folders with names that look like they should refer to drives). In any case the path definitely shouldn't begin with a colon.
Thanks for the reply. Yes, I know it looks garbled but they are actually folders on a second hard drive. After I setup 2nd Hard drive and setup a “home’ folder on it, I read it is ill advised to setup another ‘home’ folder on a second drive at the top level, so I created another top level folder to put it in so I could differentiate between my startup disk.
Yes, script works now that I removed the colon at beginning. Thank. You!
I'm curious, is there a simple way to add a notification in a script or is it easier to use a notification in mail? Trying to learn a bit about whats efficient etc. Thanks again!!
See the first version of the script at the top — there's a "display notification" line that you can edit as needed and copy into the current version of the script that you're using.
Thank you very much for your help!