Automatically Save Attachments in Mail.app

At the request from  :   Brian Jones

I wrote an Applescript for a Mail.app Rule that will Automatically Save Attachments in Mail.app .

 (* For Yosemite : please see Reported issues below *)

AutoSaveMail

The script uses the mail senders email address to gives each sender their own attachments folder.

Each messages attachments from the sender is then placed in a time stamped folder. The time stamp derives from the date received of the message.

I have not put in any code to mark the message as read or processed. But you can do this if you want.

 

Applescript Editor

Open script in Applescript Editor

 

 1- Where to save the Script file

Unlike previous Os versions where you could place your Applescript any where in the file system.  In 10.8 you need to place Mail.app Applescripts in:
~/Library/Application Scripts/com.apple.mail/
You can simply copy the above path, open a new Finder window.
Hit the keys Cmd + Shift G A sheet will slide down. Past the path in the text field and hit Go.
you will be taken to the folder. Place you Applescript file inside. Screen Shot 2013-03-29 at 12.57.57
The Script will now appear for your selection in the Mail Rules. Screen Shot 2013-03-29 at 13.04.08

 

2 -- Setting your sub folder path for the saved attachment directories and files.

If you wish to change the path of where your attachments folders go. You can make a simple change to the line of code :

set folderName to "Attachments".

For example I want my attachment folders to go into:

/Users/UserName/Documents/mailstuff/mail_Attachments.

The folderName  line will look like this:

set folderName  to  "documents:mailstuff:mail_Attachments"

Notice the colons (:) they are used instead of the forward slashes (/) to mark directorie separation.

Make sure you do not put a colon at the beginning or at end of your path. They are not needed.

The path to the user home folder is set later on in the line:

set homePath to (path to home folder as text ) as text

So you only need to concern yourself with sub folders of the user home folder.

Another thing to note.

If you only have some of the directories in the folderName path or none at all. The script will create them. It will not over write any folders already existing but will add the missing intermediary ones.

Tested on : Mac Os 10.8.3 , Mail.app  Version 6.3 (1503)

Reported issues:

1, I have filed a bug report  (below) with Apple: Nov. 2014 this will most likely be  a duplicate as ALL theses types of Applescript will be affected and others have noticed it. Hopefully apple will fix this soon.

The Problem is not with the code here. so please do not post comments if you are using Yosemite and getting the error described below.**Update -- Apple have closed my Bug report on this as a duplicate. This means they are still looking at it..

Bug Report:

As of 10.9  and 10.10 ( Yosemite) you can no longer access Mail.app's 'mail attachments' from Applescript.

This means you cannot save or count them.
You will 9 times out of 10 get resulting  error "error "Mail got an error: AppleEvent handler failed." number -10000"
This appears to happen for 90% of the emails that are in an inbox, individual or consolidated ones.
This bug is reproducible when using a "get selection" Applescripts
This bug is reproducible when Performing a "Run Applescript" action in the Mail.app mail Rules.

 

2, Reported in   comment . :   Umlauts   in folder names may stop the script from completing.

The removal of  umlauts from the name solved the problem.

114 thoughts on “Automatically Save Attachments in Mail.app

  1. Hi,

    Yes if you have set everything up correctly then this may happen because of the Bug in Mail.app. We can only wait for apple to fix this. This problem is universal for every script that looks at Mail attachments in the way until then.

  2. Hi!

    I made a rule in Mail as described and I saved the script in the specific folder. There just happens nothing. I get no error message. No folders are created.

    Kind regards

    Chris

  3. Hi, Can you explain what you mean by doesn't work. If when you run the script you get an error??

    There is a bug in Mail.app that is breaking script that look for attachments. So if anyone has linked to my code it will produce the same results. ( This is the Original code ) We are waiting for apple to fix the bug.

  4. Hi!

    I tried the script for several times, but it does not work. Has somebody got another link?

    Greetz

    Chris

  5. Hi bocaboy, there appears to be a bug in Applescript or Mail which is stopping most attachments from being returned to the applescript. I am trying to pin it down so I can pass it in to apple as a bug if no one has already done so. At the moment there is no proper work around apart from maybe have a first action that moves the email first to a mailbox in 'on my mac'. I.e. Local. I will try and update here when i know more. Cheers

  6. Mark, has something changed with Yosemite and the new version of Mail? I've been successfully running your script to detach images from an account and save them, (thank you!) but with Yosemite I find the image isn't saved although the directory entries are, in fact, created. Are there any modifications that need to take place to re-enable the script?

  7. Thanks very much, Mark. I appreciate the prompt reply and guidance on how to implement my modification.
    Best,
    M.C.

  8. Hi M.C,

    There are notes at the bottom of the post where I explain about chnaging the path to what you want. All you need in to do is find the relevant code in the script and change it. (read note 2)

    your path for dropbox would be something like set folderName to "Dropbox:mail_Attachments"

    The script is written to run from a Mail rule. Read note 1 to see how to set that up so it runs on all new messages.

  9. Mark,

    Thank for sharing this idea and nice piece of work, which I just came across after a Google search. I, too, am not AppleScript expert, and I hesitate to make changes to your script, especially since it involves the overly complicated structure (in my opinion) of Apple's Mail.app. The attachments can cause, as you know, an explosion in the size of one's Mail folder in the User Library. I was deleting images before deleting the actual email, but your Applescript idea prompted me to think it would be great to save the attachments that I might care about keeping, but have them moved to a folder in my Dropbox account instead.

    Would you mind sharing how you would edit the script so that, when its run, the attachments are moved to a Dropbox folder named "Mail Attachments"? And just to affirm my lack of knowledge on Applescripts, would this run automatically on any email that I open? Or do I run the script on an individual email that has an attachment that I want to save in Dropbox?

    Thanks very much for this generous public service to Mac users!

    All the best,
    M.C.

  10. Hi,
    You can use the unix command touch but in reality it only will do the modification date. Which can be change again at any time so in my opinion is not really a good choice for this.

    The way I would go about this is to use the setFile tool that comes with Apple's Xcode developer tools. It appears that in mavericks you can now just install the command line tools without installing xcode.

    In Terminal.app type: xcode-select --install

    This will give you a popup dialogue that gives you three options:
    command line tools

    Choose install.

    I have not done it this way myself but that should work ok.

    You can then change the time stamp code or add it as a new variable:
    set timeMod to ((m as integer) & "/" & my pad(d) & "/" & y & space & my pad(h) & ":" & my pad(min) & ":" & my pad(secs)) as string

    Then change the try block like this:

    try
    save theAttachment in file (savePath)
    do shell script "/usr/bin/SetFile -d " & quoted form of timeMod & space & quoted form of (POSIX path of savePath)
    end try

    Hope that helps

  11. Thanks Mark for the quick reply.

    I meant to modify the actual file creation and/or modification date, not to add it to the file name.

    Is that possible?

    Regards,
    Julian

  12. Hi Julian,

    The variable timestamp holds the date received of the mail message.

    So you can change the line:
    set savePath to attachmentsFolder & ":" & subFolder & ":" & timeStamp & ":" & originalName

    to:

    set savePath to attachmentsFolder & ":" & subFolder & ":" & timeStamp & ":" & timeStamp & "_" & originalName

    This will add the date at the beginning of each file.

  13. Hi Mark,

    Thanks for this very useful script.

    I have modified it slightly to save the attachments in monthly folders, I'm running on Mavericks and all is okay.

    However, I'm not an applescript expert, and I would like to have the files saved with the file creation/modification dates set to the actual email dates instead of the date/time the script was run.

    Is this possible, and if it is, can you help me out?

    Thanks again,

    Julian

Hi, Please Leave a Reply