Applescript to automatically delete certain attachments after sent in Mail

Hi everyone! I am new here, and an absolute novice in AppleScript. However I just concluded that AppleScript may be the answer for what I (badly) need to do in Apple Mail. Would any of you guys be so kind of giving me some insight or (better yet, as I am totally programming-challenged) a few lines of actual code, to achieve the following... Hope you find it at least a bit interesting.

Because of my work I often need to send the same attachment to dozens of customers every day, who request basically the same information. I mean, the exact same PDF file (which changes periodically) to everyone. Typically a 2 MB file. Now, this doesn't sound too bad, but when you have to respond literally to thousands of customer inquiries every year, it really adds up and eats space, which really becomes an issue for me when I have to backup...

Now, I would love to be able to setup a rule to automatically remove a specific attachment (or at least all attachments in a message) after the message is sent, IF the attachment name is such and such...

Something like this:

1. After a message has been sent...
2. Check attachment name...
3. If attachment name matches "SomeFile.pdf" (or if attachment name is included in some kind of list)
4. Then: remove attachment

I got a nifty add-on called "Mail Act-On", which lets you add rules for sent messages in Apple Mail. It lets you set the attachment name as a condition, but there's no build-in action to remove attachments. However it offers you the option to run an AppleScript if the condition was met.

I hope my question is not too idiot 🙂 Thanks in advance for your help!!

Mac OS X (10.6.5)

Posted on Apr 1, 2011 9:01 PM

Reply
9 replies

Apr 2, 2011 4:08 AM in response to Chris Burns

It looks like Apple Mail saves attachments 2 times.
In a folder called attachments (think for speed reasons or because of spotlight/quicklook) and in the message file itself (.emlx), too.

This code searches a folder and it's subfolder for duplicated files (comparing checksums, not just filenames)
maybe this helps if you point it to your attachments folder. Only uncomment the line

*do shell script "rm " & quoted form of filepath*

when you are are sure that you want to delete all duplicated attachments... this cannot be undone... ok maybe using time machine =)

set searchthisfolder to quoted form of "/Users/<username>/Library/Mail/<account's folder>/Attachments"

--this searches for files that have been created within the last 2 days and outputs the md5 checksum

set foundfiles to every paragraph of (do shell script "find " & searchthisfolder & " -type f -ctime -2 -exec md5 {} \\;")

set foundvalues to {}

repeat with f in foundfiles

set filepath to (characters 6 through -37 of f) as text

set md5value to last word of f

if md5value is not in foundvalues then

set foundvalues to foundvalues & md5value

else

display dialog filepath

-- do shell script "rm " & quoted form of filepath

end if

end repeat

Apr 2, 2011 7:42 AM in response to hubionmac

Hi hubionmac, thanks for replying! However I don't actually want to remove all attachments, even if duplicated. What I desperately need is an Apple Mail rule or AppleScript to remove, from within Mail, the attachments from a sent message if a specific attachment name (such as "ThisFile.pdf") is found.

This was very easy to accomplish in Entourage, as the ability to remove an attachment from the sent message (if matched the name condition) was built in. However since Entourage also had the "ability" to destroy my email database about once a year, sometimes beyond repair, since a couple of years ago I very happily deleted Entourage from my computers and switched to Apple Mail 🙂

Any help with the said script would be very very appreciated. I am a total ignorant in programming / scripting so for the life of me I can't figure out how to do it!!

Apr 2, 2011 11:52 AM in response to Tony T1

Thanks for the suggestion Tony T1. It brilliantly did the trick of helping me remove a few thousand copies of the attached PDF file from past sent messages. At this very moment Mail is actively working on the task.

Now, if it just were possible to combine a rule for sent messages (or Outbox Rules as Mail Act-On calls them) with an AppleScript to automatically remove the proverbial "ThisFile.pdf" attachment right after it was sent. Better yet, to remove attachments from the message just sent if "ThisFile.pdf" is the ONLY attachment found...

Any additional ideas will be enthusiastically welcomed. I really appreciate your time and help guys.

Apr 2, 2011 1:04 PM in response to Adam Nohejl

Thanks for your input Adam. So may it really be possible that neither AppleScript not Apple Mail have the ability to: i) Identify a specific attachment from a just-sent message; ii) And then delete attachments from that message? What do you guys think?

PS - I am of course aware Adam about other methods of delivering information. We do have websites and use download links / FTP extensively to send requested information to customers. However what I do need for this particular situation is an AppleScript that, when called from a Mail rule, is able to remove attachments from a sent message... Thanks again!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to automatically delete certain attachments after sent in Mail

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.