Save Apple Mail Attachments to a Specific Folder


Updated 2018/08/24 16:10

  • A Fully Functional Macro was added below.
  • A User-Setting for the Save Destination Path was added to the macro.

Hey Jan,

This job is AppleScriptable if you're using Apple Mail.

Save the script text with the Applescript Editor on your system.

Put the script somewhere convenient.

Run the script file with an Execute AppleScript Keyboard Maestro action.

This would be easy to customize to include the name and/or email address of the student as part of the saved-folder-name, or you could have a dated folder, etc.

At the moment a single target folder is used at:

«Your-User»/Downloads/Mail_Attachments_Saved/

But that's easy to change.

If you have problems contact me off-list. <listmeister@thestoneforge.com>

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014/05/30 19:41
# dMod: 2014/09/08 16:38
# Appl: Mail.app
# Task: Save email attachments to a named folder.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Save, @Attachments
----------------------------------------------------------------
--» MAIN
----------------------------------------------------------------

try
   
   set attachmentList to {}
   set savePathHFS to ((path to downloads folder as text) & "Mail_Attachments_Saved:")
   
   tell application "Mail"
      set selected_messages to selection
      if length of selected_messages > 0 then
         
         repeat with _message in selected_messages
            set msgAttachmentList to every mail attachment of _message
            set attachmentList to attachmentList & msgAttachmentList
         end repeat
         
         if attachmentList ≠ {} then
            
            # Test for existence of savePathHFS and create it if necessary.
            try
               alias savePathHFS
            on error
               do shell script "mkdir -p " & quoted form of (POSIX path of savePathHFS)
            end try
            
            repeat with _attachment in attachmentList
               set _name to name of _attachment
               set saveFilePath to savePathHFS & _name
               if my exTant(saveFilePath) = true then
                  error "A file already exists here!" & linefeed & linefeed & saveFilePath
               end if
               save _attachment in saveFilePath
            end repeat
         else
            error "No attachments found!"
         end if
         
      end if
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   beep
   tell current application to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
   if button returned of dDlg = "Copy" then set the clipboard to e
end try

----------------------------------------------------------------
--» HANLDERS
----------------------------------------------------------------
on exTant(_path) # Takes an HFS, Posix, or ~/Posix path as input.
   local _path
   try
      if _path starts with "~/" then
         set _path to (POSIX path of (path to home folder as text)) & text 3 thru -1 of _path
      end if
      if _path starts with "/" then
         alias POSIX file _path
      else if _path contains ":" then
         alias _path
      end if
      return true
   on error
      return false
   end try
end exTant
----------------------------------------------------------------


New 2018/08/24 16:10 CDT


Download ⇢ Save Attachments of Selected Message v1.10.kmmacros (10 KB)

4 Likes