Need help with copying mail attachments to folder.

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

I've read various methods here & elsewhere, but I don't seem to be able to get this working reliably.

The cost I have is this script
[code#copyright Mark Hunte 2013
# source: http://www.markosx.com/thecocoaquest/au ... -mail-app/

using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule


-- set up the attachment folder path
tell application "Finder"
set folderName to "Dropbox:Work:DestinationFolder"
set homePath to (path to home folder as text) as text
set attachmentsFolder to (homePath & folderName) as text
end tell


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(d) & "-" & my pad(m as integer) & "_" & my pad(h) & "-" & my pad(min)) as string -- month as number
set timeStamp to (y & "_" & my pad(d) & "-" & m & "_" & my pad(h) & "-" & my pad(min)) as string

set attachCount to count of (mail attachments of eachMessage)
if attachCount is not equal to 0 then
-- 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
repeat with theAttachment in eachMessage's mail attachments

set originalName to name of theAttachment
set savePath to attachmentsFolder & ":" & subFolder & ":" & timeStamp & ":" & originalName
#set savePath to attachmentsFolder & originalName
try
save theAttachment in file (savePath)
end try
end repeat
--on error msg
--display dialog 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
[/code]

But this script creates subfolders by email sender that, due to lack of Applescript skills, I've been unable to remove from the script and keep the code valid.

Also, when multiple emails come in at the same time only some of the attachments seem to get copied to my folder.

Also, when multiple attachments have the same name (e.g. winmail.dat) they overwrite each other.

Does anyone have a simple, reliable script that will simply copy ALL the attachments in an email to a specified folder, and allow for renaming duplicates instead of overwriting them?

Thanks,
Mark.
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am

Hopefully someone here can help but if not, you might want to try a more AppleScript-specific forum.
Mr_Noodle
Site Admin
 
Posts: 11951
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support