Mail rule Run AppleScript errors when accessing message properties
I'm trying to write a Mail rule that uses AppleScript to save a message's sender, subject, and content to a file.
To test, I can manually invoke the AppleScript from Mail on a single message with Message > Apply Rules, but it encounters errors when trying to access message properties, with this output being written to the file:
Can’t get «class mssg» id 319446 of «class mbxp» "Test" of «class mact» id "6BB74CFB-E769-4E7B-884E-30800DDDDB68".
Can’t make «class subj» of «class mssg» id 319446 of «class mbxp» "Test" of «class mact» id "6BB74CFB-E769-4E7B-884E-30800DDDDB68" into type Unicode text.
Can’t make every character of «class ctnt» of «class mssg» id 319446 of «class mbxp» "Test" of «class mact» id "6BB74CFB-E769-4E7B-884E-30800DDDDB68" into type Unicode text.
The AppleScript is based on the one created within ScriptEditor using File > New from Template > Mail > Mail Rule Action.scptd.
I've modified it as follows to try to debug it by running it within the ScriptEditor.
using terms from application "Mail"
on run
tell application "Mail"
set these_messages to selected messages of first message viewer
set the_file to (((path to desktop folder) as string) & "mailtest.txt")
set the message_count to the count of these_messages
repeat with i from 1 to the message_count
set this_message to item i of these_messages
my trace("message " & i)
my save_message(this_message, the_file)
end repeat
end tell
end run
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
set the_rule_name to name of this_rule
set the_file to (((path to desktop folder) as string) & the_rule_name & ".txt")
set the message_count to the count of these_messages
repeat with i from 1 to the message_count
set this_message to item i of these_messages
my trace("message " & i)
my save_message(this_message, the_file)
end repeat
end tell
end perform mail action with messages
to save_message(this_message, the_file)
-- GET THE SENDER OF THIS MESSAGE
try
set this_sender to the sender of this_message
if this_sender is "" then error
on error errStr number errorNumber
set this_sender to errStr --"NO SENDER"
end try
my trace("sender " & this_sender)
-- GET SUBJECT OF MESSAGE
try
set this_subject to (subject of this_message) as Unicode text
if this_subject is "" then error
on error errStr number errorNumber
set this_subject to errStr --"NO SUBJECT"
end try
my trace(this_subject)
-- GET CONTENT OF MESSAGE
try
set this_content to (every character of content of this_message) as Unicode text
if this_content is in {"", "?"} then error
on error errStr number errorNumber
set this_content to errStr --"NO CONTENT"
end try
my trace(this_content)
my writeTextToFile(this_sender & return & this_subject & return & this_content & return, the_file, false)
end save_message
end using terms from
on trace(theText)
-- say theText -- for debugging when run as Mail rule
log theText -- for debugging when run inside Script Editor
end trace
-- writeTextToFile (not shown) is from https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html#//apple_ref/doc/uid/TP40016239-CH58-SW1
If I run it manually within ScriptEditor it works correctly. The errors above occur, however, when it's run as a Mail rule.
Any ideas? (I'm also curious where logs go when run as a Mail rule)
Thanks,
Peter
iMac 21.5-inch 2019 Big Sur 11.1
ScriptEditor 2.11 (225)
iMac 21.5″, macOS 11.1