Automatically Save Attachments in

At the request from  :   Brian Jones

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

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


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 Applescripts in:
~/Library/Application Scripts/
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:


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 ,  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’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 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.

135 Replies to “Automatically Save Attachments in”

  1. Hi Rachel,

    I had a look at this. Although Apple supply a way of saving messages as RTF from the menu system there does not seem to be a way to do it from applescript without GUI scripting. Which I am not keen on.The main problem is the text format will be lost when accessing it from the email. I can show you how to save the file as a .eml file which will retain all formatting but will act like a normal email file. Including only showing images if connected to the internet. Not ideal for offline storage. This is done by getting the message “source” and saving it as a .eml file. Given sometime I may find way to do whats seems impossible now 😉 but do not hold out much hope.

    Best regards

  2. Hi Mark, this script looks great, however I am looking for a script that will save the body of an email to an rtf file. Any ideas how I could do that? I want to name the file with: senders name-mmdd-subject. I would really appreciate your help. Ideally I would like to be able to do this with sent emails as well which would need to be renamed recipients name-mmdd-subject.

  3. Hi Mark,

    Awesome. Thanks for taking the time to put that together.

    Now I just have to figure out why my mail rules stopped working. Your script worked fine for a day or two and then it stopped working. Rules are not being applied, even when doing it manually.

    I don’t think this is a problem with your script. I had the same thing happen with another script, which led me to go looking for a new one and found this post.

    There is something wrong in Mail 6.3 / OS X 10.8.3 where rules stop working for some strange reason. There are a lot of forums discussing the problem but nobody seems to have a solution. It might be a software bug.

    Just thought I should mention it in case anybody else had this problem.

  4. Hi Callum,

    Here is the change in the code you need to do.

    change the line set subFolder to (sender of eachMessage)

    set subFolder to (do shell script "echo " & quoted form of (extract address from sender of eachMessage) & " | awk -F@ '{print $2}'")

  5. This works like a charm. Is it possible to change the sender folder from the sender to just the sender’s domain name? For example, for an email from “” it would create a subfolder called “”?

  6. Had a quick look.

    try this on some test emails..
    It does not discriminate. You will have to work that out. As in it only deletes emails that you want it to. As is it will delete what ever email the rule runs on. This is just to give you an idea..

    Hope it helps.

    add this handler

    on deleteMessage(theMessage)
    tell application "Mail" to delete theMessage
    end deleteMessage

    and call to it

    my deleteMessage(eachMessage)
    as shown

  7. Thomas, you might have more success if you use the script as-is and use Hazel to process the files once they’re extracted from the mail account. Hazel is a pretty amazing piece of software that can manipulate files in almost any way if they’re on your drive.

    Just a thought!

  8. Mark, you’re welcome. If there’s a change to that I’ll let you know.

    I do have one question. (Keep in mind I’m not a programmer!) I’d like to delete the message after the attachments are saved. I tried doing that with the same rule that runs the script, but when I save it, the order of the two commands changes, and the message is moved to the Trash first and then the script is run as opposed to how I entered it. I think the pragmatic approach would be to simply put a line at the end of the script to delete the message, but I don’t know the syntax.

    Thanks, Mark!

  9. I have been trying this script and others with no success 🙁 All I want is for incoming mail to automatically save a .pdf file to my computers hard drive to a folder on my desktop. I dont need it to create any new subfolders or organize it within the mac mail app. I am running OSX 10.8.3 with mail 6.3. I have changed file names etc and feel like I am going in circles now with no success. Please help. I look forward to getting this work soon.

  10. Hi Bocaboy,
    I am glad the script worked for you. And thank you for the heads up on it working with the next the versions of Os and Mail

  11. Thanks so much, this works like a charm. I’ve tried other scripts and there were always problems. This one worked out of the box!

    For the record, I’m using a pre-release of 10.8.4 with Mail 9.5.

  12. Oops, sorry about that. I use that for testing..

    I have corrected the script I posted.

    Can you send me an example of the email. (take out the extra @s )

    Also the format of the file you want to go into the label printer folder which I assume will be a text file.
    All should be easy enough to do. No money required 🙂

  13. Hi Mark – thanks!!! I understand what you are saying re the unique filename, but each PDF already has a unique filename so not a problem.

    I tried running the script but didnt work properly, but when I remmed out
    “tell application “Mail” to set theMessages to (get selection)” it worked perfectly, just as I wanted – not sure why, but at least now its doing everything I want, so thanks again!!

    Perhaps you would like to help me with the next step that I would like to accomplish?

    Basically, when I get an order through my webstore, I get an email notification of the order along with a pdf copy of the invoice (which is what I auto print from above). What I would like to do, is to strip to the delivery address details from the email text or the pdf(it appears on both and obviously always in the same place) and then use this to auto print to a separate label printer. This way when I get an order, the invoice is automatically printed for me and also an address label.

    Can and would you be interested in helping? I dont even mind giving you some money!!

    Thanks again,


  14. Hi Daryl,

    here is a scrunched down version that will check for any pdfs and save them to the named attachments folder. I have taken out all timestamps and sub folders. The one caveat to using a single folder and no time stamps on the filenames will be that any attachments with the same name will be overwritten.

    Using timestamps in a sub folder name of the original file name is a quick way of avoiding this.

    Click to Open in Applescript Editor

  15. Hi Mark, I am hoping you can help me – I can run your code fine and works well, however, I want to simplify it, but I am struggling!!

    Basically, I have a mail rules action that selects the emails I want (PDF orders attached to email) and I simply want to save the PDF’s from these orders into a single folder with original filename and no sub folders. From here an automator folder action will print the pdfs for me.

    I have tried a load of different variations, but just cannot seem to get working – can you help please?

  16. Hi Hardy,

    I am glad you got it working on your system.
    Normally I run scripts like this with a substitution handler to take care of bad characters in email subjects field which I use for folder names. e.g “/” “:”, “;”
    The characters can interfere with the folder creation. But I cannot do the same for existing folders or trying to do it for the created folders. There would be too much to consider and no way for me to locally test it.

    But very good catch. So thank you.

  17. I found the problem: it’s a german system, and some folders contain german umlauts – this seems to be a problem. So I changed the folder names, and it works now.

  18. Hi, great script, but I still have a problem (I added some “display dialog” statements to trace):
    – The script is triggered, and it starts running (the trace between ‘tell application “Mail”‘ and ‘repeat with…’ fires).
    – The rule itself catches the mail, as it is flagged green.
    – After the “set attachmentsFolder” in ‘tell application “Finder”‘, the right path is displayed.

    However, nothing happens, as the script does not go into the “repeat with eachMessage…” loop.

    Any idea why?

    Thanks a lot,

  19. Hi John,

    I am glad it helped. if you need anything else just ask.

    And you never know one day I may take you up on that drink.

    All the best.


  20. Hi Mark: You’re the best! Just what I need. I’m going to compare the two scripts to see what you did, and hopefully I’ll learn something. I spent the better part of this afternoon trying to script on the filemaker pro 12 end to save the pdfs with the date in the filename, and man did I feel stupid. Anyway, thanks again, and if you ever get to Virginia City stop by the Mark Twain and I’ll buy you a drink–it’s the least I can do. Cheers, John

  21. Woah–this rocks! Thank you so much for this as it is going to save my daughter loads of time for daily reports she receives from our saloon’s iPad cash registers. I would like to know if it is possible to have the date append before the file name? In my case, the file inside the created subfolder of the time stamp is BarBankCountSheet. Is there a way to have the time-stamp incorporated to the file name to look something like 2013-04-24-06-32BarBankCountSheet.pdf (year-month-day-hour-minuteBarBankCountSheet) and not be in a sub-folder of time-stamp but just inside the sender folder? I’d like to use Hazel to watch the sender folder and then move the file to another folder. Having said that, my daughter is the one who knows all about Hazel, and it might be possible to search the time-stamp folders within the sender folder and rename it with the time-stamp of the sub-folder, but I’m not sure. I’m just so excited for this part of automating her needs that I wanted to ask before seeing her, so she’ll really think her old man is a genius! Seriously, this is a great, useful script. Thanks a lot. (And I’m not even going to pretend that I wrote it because she would know that wasn’t true. But I did find it.)Thanks again, John.

  22. Hi, i will try tmrw. and return to you. what i tried is a second rule. with a different extraction script, which includes some pop up messages regarding script progress. those messages show proper execution. so maybe less around the rule and starting the script but rather more something around saving exchange attachements?!

  23. Did you try it in a new user account. I do not have an exchange account. But do have a load of IMAP.starting to wonder if it could be some property in the exchange emails that is different.

  24. i just have this one rule. Tried in the meanwhile some other actions than the green flag. all those (other mark-up, tones, etc.) work properly, just the script executing or more precisly the storage of the attachment fail for the exchange one.

  25. i did 2., now on 10.8.3. and 6.3

    the behaviour did not change, confirmed is:
    – green flag appears on both account inboxes
    – attachment are only extracted for gmail

  26. Are you saying. if you set the rule to one account. You rules work. But not when you set for all messages?. Rules should work on all accounts in most of the conditions.

  27. Ok.
    I can sort the target folder out no problem. I actually already wrote the code the other day.

    But lets see if we can solve you mail rule problem first. ( no promises)

    Try and create a new user, set up mail in it. and see if you mail rules work.

    2, if that does not work. You could try and update you Mac to 10.8.3 and Mail to 6.3 and see if that fixes your system

  28. One addional/maybe important oberservation i just made: I have two inboxes (one google/imap and one exchange):
    – for the google all works (including automatic attachment download)
    – for the exchange only the green mark-up appears, but no download follows

    Any idea? Are rules account specific. I created the rule while being on the aggregated inbox (valid for both inboxes).

  29. 1. nothing changes. I tried to change target folder but this didn’t worked either, so i copied the origina code again, new script file name etc… Beside your script i have the same issue/behaviour with other extraction scipts i tried, so less script specific?!

    2. OS X 10.8.2

    3. Mail 6.2 (1499)

    4. i read the comment with the reboot and did this already.

  30. Hmm. very odd.

    I just did a test with flagging. Worked ok.

    1, Have you adjusted the script in any way.
    2, what OS X are you using
    3, what Mail Version
    4, in a comment from boni/macxbo he had to do a reboot, which he should not have to have done. But after it started working.

  31. Hi Mark,

    thanks. Rule applies to “all e-mail” and does/should execute the script. Additionally i defined to mark the mail with a green flag on. This green flag appears (so the rule seem to be somehow active), but as described the attachement apprars in the attachments folder only by applying the rule manually (again).


  32. Great script. But 1 prob, 1 question:
    – Prob: setting up the rule in mail, the app asks me to apply. in this case it works; when a new mail hits the inbox the (active) rule does not execute the script/save the attachment accordingly. manual rule apply works still.
    – Question: What do i have to change if i do not want the sender and date folders, but all attachments in one folder with the file name: ” ”

    Thank you ver much for help, i am new and so not know to much about the rules and scripts.

  33. Hi Chris,
    you can just change the line: set folderName to “Attachments”


    set folderName to “Documents:EagleFiler Libraries:Steele Communications:To Import (Steele Contracts 2012)”

    I will put a small update in the note section to explain this to others.

    Many thanks.

  34. Hi there. This is fantastic. Appreciate your efforts!
    I’m trying to change my destination to my EagleFiler “To Import” Directory.
    Could you please help me. The path is HDD\Users\chris\Documents\EagleFiler Libraries\Steele Communications\To Import (Steele Contracts 2012). Thanks in advance! 🙂

  35. Hi Ryan,

    The Applescript will run on any messages the rule deems meets its rules. In this example I use ‘Every Message’ as a filter. But you can use any of the rules to do what you want. For example ‘From’ ‘Contains’ “my The only part you would need to change in the script is the path to your Dropbox folder. If you do not know how to change that let me know along with the full path you want and I will show you the code.

  36. Is there a way to get this to only apply to certain email addresses? I’d like to automatically save monthly utility bills and statements from 3 or 4 email addresses to an actions folder in dropbox to process them with Hazel. I am a complete applescript newby with essentially no scripting experience. Thanks for this!

Hi, Please Leave a Reply