Heya,
since I updated to Yosemite (I now wish I never did) my Mail Script for saving attachments stopped working with the Scripting Error
"Error Number: -10000
Mail got an error: AppleEvent handler failed."
Any help would be appreciated.
========================
on run
set datum to current date
set monat to text -2 thru -1 of (â00â & (month of (datum) as integer))
set tag to text -2 thru -1 of (â00â & (day of (datum) as integer))
set datum to tag & â.â & monat & â.â & year of datum
set pfad to path to desktop as alias
set rootpath to POSIX path of pfad
tell application âMailâ
set theSelection to selection
repeat with theSelectedMessage in theSelection
if (count of every mail attachment of theSelectedMessage) > 0 then
set thesubject to subject of theSelectedMessage
if thesubject is "" then
set thesubject to "(kein Betreff)"
end if
set thesubject to my replace_string(my replace_string(thesubject, ":", "-"), "/", "-")
tell application "Finder"
if not (exists folder datum of pfad) then
make new folder at pfad with properties {name:datum}
end if
if not (exists folder thesubject of folder datum of pfad) then
make new folder at folder datum of pfad with properties {name:the thesubject}
end if
set mypath_original to folder thesubject of folder datum of pfad as string
end tell
try
repeat with a in (every mail attachment of theSelectedMessage)
set mypath to mypath_original
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)
set mypath to mypath & current_a_name
save a in file (mypath)
end repeat
on error msg
do shell script "echo " & quoted form of msg & " | cat>>" & quoted form of rootpath & "MailscriptErrorLog.txt"
end try
end if
end repeat
end tell
end run
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 testn to (last word of n)
repeat with c from 1 to the number of characters in testn
if (character c of testn) is â.â then
set pkt to c
end if
end repeat
set suffixlenght to ((number of characters in testn) - pkt + 1) * (-1)
set n to ((characters 1 through (suffixlenght - 1) of n) & " 1" & (characters suffixlenght 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
on replace_string(itemname, searchstring, replacestring)
set old_delimiter to AppleScriptâs text item delimiters
set AppleScriptâs text item delimiters to searchstring
set the item_list to every text item of itemname
set AppleScriptâs text item delimiters to replacestring
set this_text to the item_list as string
set AppleScriptâs text item delimiters to old_delimiter
return this_text
end replace_string
Browser: Firefox 38.0
Operating System: Mac OS X (10.10)
Iâm running Yosemite 10.10.3 and your script behaved flawlessly with attached flat-files.
I didnât tried with attached packages or folders because I never attach this kind of object. When I must send one I zip it before attaching.
Yvan KOENIG (VALLAURIS, France) jeudi 18 juin 2015 11:58:27
thank you for your answer
It seems it is not working with IMAP at all - POP and Exchange Accounts work perfectly.
What changed from 10.9 to 10.10 with IMAP?
Right, when I tested on an IMAP box the script failed.
I assume that there is a bug.
I ran the code below (subset of a larger script) after selecting one message with two attachments from my iCloud account.
It did the job.
I tried to apply it to a message with 6 attachments. One of them is a package and the script didnât treat them.
An error -10000 was issued but it was filtered by a try - end try block.
Itâs the exact error which I got with your code.
set pToD to path to documents folder
set attachmentsFolder to (pToD as text) & "Attachments:" # avec caractĂšre ":" final
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of attachmentsFolder)
tell application "Mail"
set theMessages to selection
set mailSubFolder to false # DĂ©finit la variable mailSubFolder par dĂ©faut afin qu'elle existe mĂÂȘme s'il n'y a pas de piĂšce jointe
repeat with eachMessage in theMessages
set {senderName, sender_Mail, TopicSubject_, mailDate_} to my getMailInfos()
set dateStamp to space & my makeStamp(mailDate_)
# On avait mis le caractĂšre ":" final
set mailSubFolder to attachmentsFolder & ":" & senderName & ":" # Avec caractĂšre ":" final
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of mailSubFolder)
# Enregistrement des piĂšces jointes
# S'il existe dĂ©jĂ un fichier du mĂÂȘme nom, ajoute la date-heure du mail pour diffĂ©rencier
try # Filter the error issued when an attachment is a folder or a package
repeat with theAttachment in eachMessage's mail attachments
try
# On avait mis le caractĂšre ":" final
set attachmentName to name of theAttachment
tell application "System Events"
if exists disk item (mailSubFolder & attachmentName) then
if attachmentName ends with ":" then set attachmentName to text 1 thru -2 of attachmentName
if attachmentName contains "." then
set nameAsList to my decoupe(attachmentName, ".")
set attachmentName to my recolle(items 1 thru -2 of nameAsList, ".") & dateStamp & "." & item -1 of nameAsList
else
set attachmentName to attachmentName & dateStamp
end if
end if
end tell
save theAttachment in file (mailSubFolder & attachmentName)
end try
end repeat
end try
end repeat # with eachMessage.
end tell # Mail
#===== Called from treatAmail
#
on getMailInfos()
# define the local variables whose value will be send to the caller
local sender_Name, sender_Mail, mail_Subject, mail_Date
log return & "Entering the handler : getMailInfos"
tell application "Mail" to tell item 1 of (get selection) # extracts datas from the selected message
set sender_Name to extract name from its sender
set sender_Mail to extract address from its sender
set mail_Subject to subject
set mail_Date to date received # ATTENTION, conserver la composante ËtimeË
end tell # Mail
log return & "Leaving the handler : getMailInfos"
# send the extracted values to the caller code
return {sender_Name, sender_Mail, mail_Subject, mail_Date}
end getMailInfos
#=====
on decoupe(t, d)
local oTids, l
set {oTids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTids
return l
end decoupe
#=====
on recolle(l, d)
local oTids, t
set {oTids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTids
return t
end recolle
#=====
on makeStamp(aDate)
tell aDate to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end makeStamp
#=====
Yvan KOENIG (VALLAURIS, France) vendredi 19 juin 2015 10:57:33