Observations and Productivity Gains - Tweaking the Autofiling Script

I continue to analyze my productivity by observing where I have little frustrations or moments of confusion in my work. My Filing System has been a net positive. However, I've noticed over time, that often I'm saving files 2 or even 3 times from my email. Once I made this observation, I tried to understand why. In this case it was simple. I would be in a hurry and saw a file in my email I needed to save, I'd activate the script and leave the email in my inbox. When I touched the email again, say to archive it, I would often hit the script again because I didn't know if I'd saved the file.

The fix is simple, I needed to move the email to a processed folder to know that I had saved the file. I tried doing this manually to maintain flexibility in where I put the email. However, this still caused friction so I decided to automate this at the end of my script.

Per my usual modus operandi, one of the first link in a google search was a post by Ben Waldie to move emails on Mail. Since Ben clearly knows his stuff. I found the key bits and jammed them in my script. See below for the new bit and the full script. Problem solved. I liked how this worked so I also added 2 separate scripts to move email I might need to a temporary holding file and those emails I know I wanted to keep. So far, I'm touching my emails fewer times.

This does require some thought because if I want to send the email to Omnifocus, I need to do this before I file and archive the email via my script.

Screen Shot 2014-01-18 at 3.49.48 PM.png
-- 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

set subName to (sender of eachMessage)
set AppleScript's text item delimiters to "<"
set fName to text item 2 in subName
set AppleScript's text item delimiters to "@"
set fName to text item 1 in fName
set AppleScript's text item delimiters to "."
set subFolder to text item 1 in fName


-- 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