I have been using the following AppleScript since 2006, through every OS upgrade, and I cannot discover why it will not function now:
using terms from application "Mail"
on perform mail action with messages The_Messages
my logit("PhoenixReportSave04 fired", "phxLog")
try
set today_folder to my CheckOrMakeFolder((current date)'s short date string)
tell application "Mail" to repeat with This_Message in The_Messages
set Report_name to ((every word of (get This_Message's subject)) as rich text) & ".pdf"
set Report_file to my checkFileName(today_folder, Report_name)
save first mail attachment of This_Message in Report_file
end repeat
on error errText number errNum
my logit(("Error: " & errText & errNum), "phxLog")
end try
end perform mail action with messages
end using terms from
The purpose is to save PDF attachments of laboratory reports on patients into a specified folder based on the received date. I added the logit handler to find the problem, and here is what it returns:
2012-08-30 17:17:49: Error: Can't get «class mssg» 1 of «class mbxp» Incoming POP Messages of «class mact» Cathouse Main. Invalid index.-1719
Since it appears that the repeat is the problem, I put together a modification as a testing script with a conditional to test for the single message state, and process it in that fashion, but I get the exact same error, even though the conditional works as written:
using terms from application "Mail"
on perform mail action with messages The_Messages
set messageCount to (count The_Messages)
my logit("MailSolutions fired with " & (messageCount as rich text) & " message(s)", "ScriptTestingLog")
try
if messageCount > 1 then
tell application "Mail" to repeat with This_Message in The_Messages
my logit("Accessing multiple conditional", "ScriptTestingLog")
set Report_name to ((every word of (get This_Message's subject)) as rich text) & ".pdf"
end repeat
else
tell application "Mail"
my logit("Accessing singleMessage conditional", "ScriptTestingLog")
set singleMessage to (item 1 of The_Messages)
set Report_name to ((every word of (get singleMessage's subject)) as rich text) & ".txt"
end tell
end if
my logit(Report_name, "ScriptTestingLog")
on error errText number errNum
my logit(("Error: " & errText & errNum), "ScriptTestingLog")
end try
end perform mail action with messages
end using terms from
----------------------------------------------------------------------
to logit(log_string, log_file)
do shell script ¬
"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit
Result:
2012-08-30 18:29:57: MailSolutions fired with 1 message(s)
2012-08-30 18:29:57: Accessing singleMessage conditional
2012-08-30 18:29:57: Error: Can't get «class mssg» 1 of «class mbxp» Incoming POP Messages of «class mact» Cathouse Main. Invalid index.-1719
Does anyone have any idea what to try next?