Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I am trying to automatically save all email sent to one address (let's call it test@example.com) to this folder /Users/luomat/Dropbox/Incoming/e2w/ (where /Users/luomat = $HOME).

Here is the rule that I am using in Mail.app:

enter image description here

I have setup a special email address for this purpose, and am fetching it via POP3 from Gmail. The message is being colored Red and marked as read, but the AppleScript does not seem to be working.

I have tried two "Save Mail" AppleScript attachments that I found online, but neither of them work.

(You can find them at http://share.luo.ma/temp/mail-app-applescript/ if you want to see them.)

I am wondering if perhaps Sandboxing rules in Mail.app 10.8 are preventing this from working, or if I am just doing it wrong. I'm not sure how to troubleshoot AppleScript from Mail.app rules, and I don't really know AppleScript, I've just edited those two scripts I found online to fit what I want to do.

Here's what I am trying to accomplish (Ideally):

  1. If the email is plain text, just save the message to /Users/luomat/Dropbox/Incoming/e2w/ using something like "Subject Line from email address.txt"

  2. If the email has attachments, create a folder in "/Users/luomat/Dropbox/Incoming/e2w/" using the Subject of the email as the folder name, and then save the body of the email as text in that folder as "body.txt" and save the attachments as whatever they were originally called.

Note: after this AppleScript the /Users/luomat/Dropbox/Incoming/e2w/ will be automatically processed and files will be moved out of that folder using Hazel, so I don't really need to worry about filename collisions.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Try this.

   using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        -- The folder to save the attachments in (must already exist)


        -- Save in a sub-folder based on the name of the rule in Mail

        set subFolder to name of theRule
        tell application "Finder"
            set attachmentsFolder to ((path to home folder as text) & "Dropbox:Attachments") as text
            if not (exists folder subFolder of folder attachmentsFolder) then
                make new folder at attachmentsFolder with properties {name:subFolder}
            end if
        end tell
        tell application "Mail"

            repeat with eachMessage in theMessages

                set {year:y, month:m, day:d, hours:h, minutes:min} to eachMessage's date sent
                set timeStamp to ("" & y & "-" & my pad(m as integer) & "-" & my pad(d) & "-" & my pad(h) & "-" & my pad(min))

                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
                        try
                            save theAttachment in file (savePath)
                        end try
                    end repeat

                    display dialog subFolder
                end try
            end repeat

        end tell
    end perform mail action with messages
end using terms from

-- Adds leading zeros to date components
on pad(n)
    return text -2 thru -1 of ("00" & n)
end pad

I have posted a more general Automatically Save Attachments in Mail.app on my blog

share|improve this answer
    
This works to save the attachments. It doesn't save the body of the email, but that's less important to me. Many thanks! –  TJ Luoma Nov 13 '12 at 14:26
    
Excellent. I seem to get a confirmation dialog every time this filter runs - do you know how it can be disabled? –  magma Jan 2 '13 at 11:03
    
remove or comment out "display dialog subFolder". To comment out put -- before it –  markhunte Jan 2 '13 at 11:54
    
I have posted a more general Automatically Save Attachments in Mail.app on my blog markosx.com/thecocoaquest/… –  markhunte Mar 24 '13 at 18:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.