-- hubionmac.com 2010-01
-- script to be added to a Apple Mail rule
--saves attachmens of new mails into a folder
on perform_mail_action(info)
tell application "Mail"
tell application "Finder" to set mypath to (folder "Mail Attachments Saved" of desktop) as text
set theMessages to |SelectedMessages| of info
repeat with thisMessage in theMessages
try
repeat with a in (every mail attachment of thisMessage)
set current_a_name to name of a
set current_a_name to my checkname_with_pdf_suffix(current_a_name, mypath as alias, false)
save a in mypath & current_a_name
end repeat
on error msg
do shell script "echo " & quoted form of msg & " | cat>>~/Desktop/MailscriptErrorLog.txt"
end try
end repeat
end tell
end perform_mail_action
on checkname_with_pdf_suffix(n, D, looped)
tell application "Finder"
set thefiles to name of every item of (D as alias)
end tell
if thefiles contains n then
if looped = false then
set n to ((characters 1 through -5 of n) & " 1" & (characters -4 through -1 of n)) as text
my checkname_with_pdf_suffix(n, D, true)
else
set tmp to (last word of ((characters 1 through -5 of n) as text) as integer)
set tmpcount to (count of characters of (tmp as text)) + 5
set tmp to tmp + 1
set n to ((characters 1 through (-1 * tmpcount) of n) & tmp & (characters -4 through -1 of n)) as text
my checkname_with_pdf_suffix(n, D, true)
end if
else
return n
end if
end checkname_with_pdf_suffix