PDA

View Full Version : Auto save & rename email attachment on receipt




Philgr
Apr 15, 2012, 06:56 AM
I wonder if anyone can help

I receive an email once a week from a specific client with a copy of an invoice attached

I would like to upon receipt of this email automatically rename the attachment with the date and save it to a specific folder location

i know i can set up a rule on the account / email address and run a script, but my scripting knowledge is Zero

can anyone suggest a solution

Thanks in advance

Phil G



kryten2
Apr 15, 2012, 01:57 PM
(*
Sample Rule Action Script

Copyright 2003 Apple Computer, Inc.

The code was descended from Apple sample
code, and sample code by Benjamin S. Waldie.
*)
(*
This script is an example of how to write an AppleScript that can be
attached as a rule action. See Mail Help for details and Mail's
AppleScript dictionary for the full terminology for the 'perform mail
action with messages' handler.

If you attach this script to a rule action, and the rule
matches an incoming message, the attachment will be saved to
the folder you specified.
*)

--Choose your specific folder location
set theOutputFolder to (choose folder with prompt "Choose folder to save mail attachment :" default location (path to desktop)) as string
--If your client's name is Billy Bob and you've
--made a folder with name "Billy Bob" on your
--desktop and you choose that folder the result will be:
-- "Macintosh HD:Users:yourusername:Desktop:Billy Bob:"
--Or change the first set theOutputFolder ... to
--set theOutputFolder to "Macintosh HD:Users:yourusername:Desktop:Billy Bob:"
--for hardcoded path
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set theAttachments to every attachment of content of eachMessage
repeat with a from 1 to length of theAttachments
set theAttachment to item a of theAttachments
try
set theAttachmentName to name of theAttachment
set theSavePath to theOutputFolder --& theAttachmentName
save theAttachment in theSavePath
end try
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from

Unfortunately the name property of mail attachment is read only so you'll have to do the renaming bit after you saved the attachment. This can be solved by an automator folder action.

In Automator :

Rename Finder Items --> add date or time
Save as plugin --plugin for folder actions --attach to folder "Billy Bob"

This is on Leopard. Things have changed for Automator in Snow Leopard but shouldn't be to difficult to figure out.
Script is not tested.

Philgr
Apr 16, 2012, 12:21 AM
Many thanks will give this a go tonight

:)

Phil G

kryten2
Apr 16, 2012, 09:49 PM
I tested the script and it didn't work.
This one works.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
-- Only use hardcoded path! DO NOT USE choose folder. It will crash Mail if you do
set theOutputFolder to "Macintosh HD:Users:yourusername:Desktop:Billy Bob:" -- replace quoted text with your desired path
tell application "Mail"
repeat with theMessage in theMessages
if theMessage's mail attachments is not {} then
repeat with theAttachment in theMessage's mail attachments
set theFileName to theOutputFolder & theAttachment's name
try
save theAttachment in theFileName
on error errnum
end try
end repeat
end if
end repeat
end tell
end perform mail action with messages
end using terms from


Folder action script :

on adding folder items to this_folder after receiving these_items
tell application "Finder"
if not (exists folder "Attachment" of this_folder) then
make new folder at this_folder with properties {name:"Attachment"}
end if
set the destination_folder to folder "Attachment" of this_folder as alias
end tell
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to info for this_item
if this_item is not the destination_folder then
tell application "Finder"
move this_item to destination_folder
set the_file_name to name of this_item
set the_file_ext to name extension of this_item
end tell
set x to the offset of "." in the_file_name
set the_file_name to text 1 thru (x - 1) of the_file_name
set current_date to do shell script "date '+%d-%m-%Y'"
tell application "Finder" to set name of this_item to the_file_name & space & current_date & "." & the_file_ext
end if
end repeat
end adding folder items to