1

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
spartyguy
  • 11
  • 3
  • Exactly what changes did you make to the working script that is now not working? You should always show, explicitly and specifically, what was working and also what is not working. That said, `set attachmentsFolder to ("Macintosh HD:MegaStore:Sync:logs") as text` does not point to a Users folder or any external location either. Have a look at: [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – user3439894 Dec 12 '19 at 14:51
  • I only changed this line: '''set attachmentsFolder to ("Macintosh HD:MegaStore:Sync:logs") as text It worked with this line: set attachmentsFolder to ((path to home folder as text) & "Sync:logs") as text Otherwise it is the same script top to bottom – spartyguy Dec 12 '19 at 18:08
  • This _path_ `"Macintosh HD:MegaStore:Sync:logs"` points to the internal disk, not the external disk. If the name of the mounted disk is `MegaStore` then use `"MegaStore:Sync:logs"` as the _path_. – user3439894 Dec 12 '19 at 20:22
  • Was my previous comment helpful in resolving the issue? – user3439894 Dec 14 '19 at 00:03
  • I made the change and the Mail rule marks the message read, but the script doesn't save the attachments to that specified folder (or anywhere for that matter) – spartyguy Dec 14 '19 at 13:28
  • 1. What version of macOS are you running? 2. What is the fully qualified path to the target folder you are trying to save to? In Finder, try right-clicking on the target folder, then press the Option key. Do you see Copy "folder_name" as Pathname, if so click it and then paste it in a comment. If you do not see that, then drag and drop the target folder onto a Terminal window, this will show the fully qualified pathname, copy and paste it in a comment. – user3439894 Dec 14 '19 at 13:43
  • Running macOS 10.15.2, the path is /Volumes/MegaStore/Sync/logs – spartyguy Dec 14 '19 at 18:58

1 Answers1

1

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.

naven87
  • 964
  • 1
  • 7
  • 24
  • I, too, am stuck on that. I tried `save this_attachment in file saveDest` and `save this_attachment in saveDest` (= without the "file") which results in two different errors, one says something like "can't get attachment xyz" with the name it should be saving **to**, the other one sounds like a permission problem despite full disk access. Any progress? – phdoerfler Feb 09 '22 at 20:05