Save Timestamped Mail Attachments with Rules and Applescript

Jan 13, '11 07:30:00AM

Contributed by: smadad

Recently I was working on a web application that required some data from another antiquated system. As it turned out, a text file with a generic name, sent as an email attached, was the only way to get this data. Not wanting my app to require any manual intervention, I set out to figure out how to automatically save these attachments into a specific folder with a timestamp in the file name. This would allow a PHP script to easily access these files as well as verify the data is current.

Having next to zero experience with Applescript, of course I turned to my favorite site for all things Mac OS X related and found this hint, which got me most of the way there. I pieced together the rest, and thought it might be a useful addition to these archives as there doesn't seem to be anything else specifically about handling Mail attachments with Applescript.

Here's the script. I have tried to comment it clearly. Also, despite knowing many programming languages, this is my first AppleScript, so all you experts, go easy on me.

[crarko adds: Note: you'll want to edit theOutputFolder variable in the script to match whatever path you'd like to save the file to.]

Copy and paste the script into AppleScript Editor and save it in ~/Library/Scripts/Applications/Mail. Then it will show up in the Scripts menu when you are in Mail. You may need to create the directory if it's not already there.

--executes when the conditions of a Mail rule are met
on perform_mail_action(theData)
  
  --define the output folder to the file path where the attachment(s) should be saved
  set theOutputFolder to "Macintosh HD:Library:WebServer:Documents:WebAppData:" as string
  
  --get the rule name
  set theRule to |Rule| of theData
  set theRuleName to name of theRule
  
  --set the file name prefix based to the Mail rule name
  --this allows this same script to be re-used for as many different Mail rules and attachment types as needed  
  --just remember to give the Mail rule the name you want the file to be saved as
  set filePrefix to theRuleName as string
  
  --get the date
  set {y, m, d, h, min} to ¬
    {year, month, day, hours, minutes} of (current date)
  
  --get the year as four digits
  set theYear to y as string
  
  --get the month as a number (1-12)
  set theMonth to (m * 1) as number
  --prepend a zero to a one digit month (for sorting purposes)
  if theMonth < 10 then
    set theMonth to "0" & theMonth as string
  end if
  
  --get the day of the month (1-31)
  set theDay to d as number
  --prepend a zero to one digit days (for sorting purposes)
  if theDay < 10 then
    set theDay to "0" & theDay as string
  end if
  
  
  --get the hour of the day (0-23)
  set theHour to h as number
  --prepend a zero to one digit hours (for sorting purposes)
  if theHour < 10 then
    set theHour to "0" & theHour as string
  end if
  
  --get the minute (0-59)
  set theMinute to min as number
  --prepend a zero to one digit minutes (for sorting purposes)
  if theMinute < 10 then
    set theMinute to "0" & theMinute as string
  end if
  
  --create the timestamp portion of the file name separated by underscores
  set theDate to theYear & "_" & theMonth & "_" & theDay & "_" & theHour & "_" & theMinute
  
  --create file's name from the prefix and the timestamp
  set theAttachmentName to filePrefix & theDate & ".txt"
  
  --script Mail
  tell application "Mail"
    --get array of all incoming messages that match the rule
    set theSelectedMessages to |SelectedMessages| of theData
    
    --loop through the matching messages
    repeat with a from 1 to count theSelectedMessages
      
      --get this message
      set theMessage to item a of theSelectedMessages
      
      --loop through all the attchments on this meessage
      repeat with theAttachment in theMessage's mail attachments
        
        --create the full save path to the new file (the output folder combined with the file name)
        set theSavePath to theOutputFolder & theAttachmentName
        
        --save the file
        save theAttachment in theSavePath
        
      end repeat
    end repeat
  end tell
end perform_mail_action

[crarko adds: I haven't tested this one. I have mirrored the script here.]

Comments (0)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110105135826467