Saving Mail attachments

Discussion in 'Mac Apps and Mac App Store' started by MiniMoke, Sep 10, 2012.

  1. MiniMoke, Sep 10, 2012
    Last edited: Sep 10, 2012

    MiniMoke macrumors member

    Nov 12, 2009
    I know, this has probably been discussed a lot, but up to now I have not been able to find an easy solution for extracting mail attachments from Mail (latest version on Mountain Lion) and save them to a specific folder (In this case the Dropbox/Attachments folder in my home directory).

    I tried Automator which can extract the attachments but can't, link to mail rules. Mail rules of course can't extract attachments and I even tried Applescript to accomplish this or to make the Automator workflow run but it didn't work.

    What exactly am I trying to do: I want to extract mail attachments from newly arrived mails with a certain subject (!Dropbox!) and save them to my attachments folder within the Dropbox folder. This should happen automatically as soon as new mail arrives.

    Is anyone, with a bit of patience and pedagogic skills, able to guide me step by step through this? Of course I'm totally new to Applescript though I have some little experience with mail rules and even Automator.

  2. Weaselboy Moderator


    Staff Member

    Jan 23, 2005
  3. MiniMoke thread starter macrumors member

    Nov 12, 2009
    Thanks a lot, but I tried the script (only adapted the location to my "Dropbox:Attachmentts"), folder but nothing happens when I send a mail with attachment.....

    No Idea where my error lies but I probably got something wrong... or is this why SendToDropbox is not working for me?? Anyone had experiences with this?

    Here's the script I pasted in AppleScript editor:

    on perform_mail_action(ruleData)

    -- The folder to save the attachments in (must already exist)
    set attachmentsFolder to ((path to home folder as text) & "Dropbox:Attachments") as text

    -- Save in a sub-folder based on the name of the rule in Mail
    set subFolder to name of |Rule| of ruleData as text
    tell application "Finder"
    if not (exists folder subFolder of folder attachmentsFolder) then
    make new folder at attachmentsFolder with properties {name:subFolder}
    end if
    end tell

    -- Get incoming messages that match the rule
    tell application "Mail"
    set selectedMessages to |SelectedMessages| of ruleData
    repeat with theMessage in selectedMessages

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

    -- Save the attachment
    repeat with theAttachment in theMessage's mail attachments
    set originalName to name of theAttachment
    set savePath to attachmentsFolder & ":" & subFolder & ":" & timeStamp & " " & originalName
    save theAttachment in savePath
    end try
    end repeat
    end repeat
    end tell

    end perform_mail_action

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

Share This Page