Update to Autofiling Script

A reader asked about altering the autofiling script talked about here to capture the name of the sender rather than the parsed bit I was using. As I had a note from Mark Hunte on a simpler way to execute the script that he sent when I first posted this. As my script is still working for me, I'd not changed it. But this has been on the list of things to try out so several weeks later I got some time and the update was indeed easy. The script is below and will save the files from an email to a nested folder structure based on the sender name.

Github repository is here - look at the Autofiling-Email-Attachments Branch

Here is what Mark ultimately suggested:

set theExtractAddress to extract name from (sender of eachMessage) set subFolder to do shell script "echo " & theExtractAddress & " |awk -F@ '{print $1}' |awk -F. '{print $1}'"

I didn't do much of any testing at all on this so please comment or send me a note if you find some errors.

-- Adapted from a copyrighted script by Mark Hunte 2013 
-- http://www.markosx.com/thecocoaquest/automatically-save-attachments-in-mail-app/
-- Changed script to parse out the first part of the email address as the folder name, eliminated time stamp folder
-- Changed to run as triggered script vs email rule
-- explanation of what and why at scrubbs.me


-- set up the attachment folder path

tell application "Finder"
    set folderName to "Attachments"
    set homePath to (path to home folder as text) as text
    set attachmentsFolder to (homePath & folderName) as text
end tell


tell application "Mail"
    
    set theMessages to selection
    repeat with eachMessage in theMessages
        
        -- set the sub folder for the attachments to the first part of senders email before a period
        -- All future attachments from this sender will the be put here.
        -- parse email name by @ and . to get to first part of email name
        
        -- updated script with suggestion from Mark at markosx.com
        -- Just wanted to give you another tip. You can extract the address without the 
        -- "fullname < email@address > " format by using the "extract address from" command.
        -- then altered to get at the name at request of reader at scrubbs.me
        
        set theExtractAddress to extract name from (sender of eachMessage)
        set subFolder to do shell script "echo " & theExtractAddress & " |awk -F@ '{print $1}' |awk -F. '{print $1}'"
        
        
        -- use the unix /bin/test command to test if the timeStamp folder  exists. if not then create it and any intermediate directories as required
        if (do shell script "/bin/test -e " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder) & " ; echo $?") is "1" then
            -- 1 is false
            do shell script "/bin/mkdir -p " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder)
            
        end if
        try
            -- Save the attachment
            repeat with theAttachment in eachMessage's mail attachments
                
                set originalName to name of theAttachment
                set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName
                try
                    save theAttachment in file (savePath)
                end try
            end repeat
        end try
    end repeat
    
-- adapted from script by Ben Waldie at http://www.peachpit.com/blogs/blog.aspx?uk=Mac-Productivity-Quick-Scripts-and-Workflows--Archiving-Selected-Mail-Messages

        set theArchiveMailboxName to "Processed"
        if (mailbox theArchiveMailboxName exists) = false then
            make new mailbox with properties {name:theArchiveMailboxName}
        end if
        repeat with aMessage in theMessages
            move aMessage to mailbox theArchiveMailboxName
        end repeat
    
end tell