After learning the ins and the outs of Alfred today I'm going to do the hard part: the actual AppleScript to save the attachments from Mail into a folder.

My previous scripts are used in one form or the other for Mail Archiver. There was very few if any error handling. The script for saving the attachments is going to be part of an Alfred workflow and is meant to be used by others users. Therefore, it needs a bit more error handling.

Because the script is longer than the other scripts I'll describe what the different parts do without showing the code for the parts.

  1. I'm going to use an environment variable for the folder where I want the attachments to go. But for now I set the folder to a fixed one. When the POSIX file can't be converted to an alias then the folder doesn't exist. Then I need to check if a folder or a file has been selected.
  2. There will be 2 environment variables for setting the name of the attachment folder. I need to check if at least one variable has been set to true.
  3. The next step is to get the selected emails in Mail. The script does a loop over the selected emails. For each email only something is done when the email has attachments.
  4. With the subject and the sender of the email the name of the attachment folder is calculated.
  5. In Finder a new folder is made with the attachment folder if the folder doesn't exist.
  6. Finally, the attachments are saved into the folder.

Here is the full AppleScript:

--set main folder of attachments
set attachmentsPath to "/Users/beatrixwillius 1/Documents/Development/Mail Archiver issues from users"
set attachmentsFolder to (attachmentsPath as POSIX fileas alias
on error errMsg number errNr
display dialog "Please select a folder for saving the attachments! Error: " & errMsg & " Nr.: " & errNr
end try
if not my isdirectory(attachmentsPaththen
display dialog "Please select a folder for saving the attachments!"
end if

--set how the sub folders for the attachments should be named
set UseSubject to "false"
set UseFrom to "true"
if not UseSubject = "true" and not UseFrom = "true" then
display dialog "Please set the name of the attachment folders either to the subject or the from of the email!"
end if

tell application "Mail"

--get the selected emails
set selectedMessages to selection

--loop over the messages to get and save the attachments
repeat with theMessage in selectedMessages

--do only something if the message has attachments
set theAttachments to theMessage's mail attachments
if (count of theAttachments) > 0 then

--make folder in Finder with the selected name option
set theSubject to subject of theMessage
set Originator to sender of theMessage
tell application "Finder"
if UseSubject = "true" and not UseFrom = "true" then
set FolderName to theSubject
else if UseFrom = "true" and not UseSubject = "true" then
set FolderName to Originator
else if UseFrom = "true" and UseSubject = "true" then
set FolderName to Originator & " " & theSubject
end if
set newFolder to (make new folder at attachmentsFolder with properties {name:FolderName})
on error errMsg number errNr
if errNr = -48 then
--folder already exists
set newFolder to get item 1 of (every folder in attachmentsFolder whose name contains FolderName)
display dialog "Error: " & errMsg & " Nr.: " & errNr
end if
end try
open newFolder
end tell

--save the attachments
repeat with theAttachment in theAttachments
set originalName to name of theAttachment
set savePath to (newFolder as rich text) & originalName
save theAttachment in file savePath
end repeat
end if
end repeat
on error errMsg number errNr
display dialog "Error: " & errMsg & " Nr.: " & errNr
end try

end tell

--check if the path is a directory and is actually there
on isdirectory(someItem-- someItem is a file reference
set filePosixPath to quoted form of (POSIX path of someItem)
set fileType to (do shell script "file -b " & filePosixPath)
if fileType ends with "directory" then return true
return false
end isdirectory

In the next blog article I'm going to make a new workflow in Alfred with the AppleScript as action. I'll need to do some minor change to the AppleScript.