I need some help with a script I wrote to run when a specific type of message is received in Mail. When a message comes in, the script runs, but stops at “set theBody to content of theMessage as rich text”. It never gets to “say 2”. If I comment out the set theBody line, it will progress to say 2.
When I apply the rule to the message manually, the whole scripts runs without any issue. Can somebody help me figure out what is wrong? I cannot seem to figure it out.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
say 1
tell application "Microsoft Excel"
activate
open "/~/Documents/Computer List.xls"
end tell
tell application "Mail"
repeat with theMessage in theMessages
say "The count of the messages is " & (count of theMessages)
set theBody to content of theMessage as rich text
say 2
-- search the body of the message and then enter the contents into Excel
repeat with a from 1 to ((count of paragraphs of theBody) - 1)
-- collect data in each line of the email
-- get the computer ID
say 3
set compID to word 1 of paragraph a of theBody
-- get the pass or fail status
try
get word 2 of paragraph a of theBody
if word 2 of paragraph a of theBody contains "f" or "F" then
set testResult to "Fail"
else
set testResult to "Pass"
end if
on error
set testResult to "Pass"
end try
-- get any notes
try
get word 3 of paragraph a of theBody
set problem to word 3 of paragraph a of theBody
on error
set problem to ""
end try
-- enter data into the Excel document
tell application "Microsoft Excel"
say 4
try
set searchRange to range ("A:A")
set foundRange to find searchRange what compID
set fRow to first row index of foundRange
set dateLoc to "C" & fRow
set resultLoc to "D" & fRow
set notesLoc to "E" & fRow
set value of range dateLoc to current date
set value of range resultLoc to testResult
set value of range notesLoc to problem
save file
on error
return
end try
end tell
end repeat
delete theMessage
end repeat
end tell
end perform mail action with messages
end using terms from
That may be because rich text is a thing from the past. Script doesn’t compile here (10.6), Editor complains about that phrase. With the rich deleted, script compiles, and might even work…
Applescript Editor automatically puts in ‘rich text’. Every time I enter ‘text’ or delete ‘rich’ it automatically puts ‘rich text’ in when I compile it.
I should also point out that I am running 10.8.2 currently.
That’s strange…seems like a regression. In 10.6, content of a message is text, so as (rich) text is not needed at all. See what happens when you delete the phrase entirely. And please check what it says in the dictionary for your Mail version.
If I remove ‘as rich text’ it still does not work. I did look in the dictionary for Mail and it shows that its type is rich text. I do know that the line is the problem because if I remove the entire line the script proceeds to say 2 as it should. I cannot figure out what is wrong with the line as it looks like it should work and does when applied manually to a message. I am a beginner at this and cannot seem to get any further.
In case anyone is interested. As a workaround for the issue of Mountain Lion not being completely compatible with Mail Rule scripts executing properly, I changed the script so that instead of searching through only the messages that apply to the rule, I had it filter through the entire Inbox and that allowed the script to run on its own without any problems. Hopefully, as Craig pointed out, Apple will provide a fix for this issue.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Microsoft Excel"
activate
open "/~/Documents/Computer List.xls"
activate object of window "Computer List.xls"
end tell
tell application "Mail"
repeat with theMessage in (every message of inbox)
if read status of theMessage is false then
if subject of theMessage is "mri" then
set theBody to content of theMessage as rich text
-- search the body of the message and then enter the contents into Excel
repeat with a from 1 to (count of paragraphs of theBody)
-- collect data in each line of the email
-- get the computer ID
set compID to word 1 of paragraph a of theBody
if (count of compID) = 4 then
try
get compID as number
-- get the pass or fail status
try
get word 2 of paragraph a of theBody
if word 2 of paragraph a of theBody contains "f" or "F" then
set testResult to "Fail"
else
set testResult to "Pass"
end if
on error
set testResult to "Pass"
end try
-- get any notes
try
get word 3 of paragraph a of theBody
set paraContent to paragraph a of theBody
set wordCount to ((count of word 2 of paraContent) + 7)
set charCount to (count of paraContent)
set problem to rich text wordCount thru charCount of paraContent
on error
set problem to ""
end try
-- enter data into the Excel document
tell application "Microsoft Excel"
try
set searchRange to range ("A:A")
set foundRange to find searchRange what compID
set fRow to first row index of foundRange
set dateLoc to "C" & fRow
set resultLoc to "D" & fRow
set notesLoc to "E" & fRow
set value of range dateLoc to current date
set value of range resultLoc to testResult
set value of range notesLoc to problem
save file
on error
return
end try
end tell
end try
end if
end repeat
delete theMessage
end if
end if
end repeat
end tell
end perform mail action with messages
end using terms from