简体   繁体   中英

Applescript to Save Attachments From Mail to External HD

From an incoming Mail message I would like to copy attachments to an external HD. I'm using a script to copy to the attachments to the User directory by way of a Mail rule that activates when it sees the email address of the sender.

I tried modifying it to copy to the External HD and it doesn't do anything as far as I can tell. Here is what I've modified, I believe the attachmentsFolder is the sticking point

on perform_mail_action(ruleData)

    -- The folder to save the attachments in (must already exist)
    set attachmentsFolder to ("Macintosh HD:MegaStore:Sync:logs") 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
                try
                    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

I am pretty sure the "try" block is masking that you can't save the attachments. I have the same problem right now, if you uncomment the "try" and "end try" lines, I'm betting you'll get an error regarding setting access in finder. I'm still stuck with that one. It seems like it's not getting write access despite have full disk access set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM