How Do I Write File from Apple Mail Rule Script

I have an AppleScript that generally works as an Apple Mail rule. Except that I can’t figure out how to write a text file from the script.

When I try to use ASObjC, I get this error long before I get to the writeFile() handler:

Exception in Mail action: (-1728) Can’t get «class subj» of «class mssg» id 212 of «class mbxp» “INBOX” of «class mact» id “AA273C3B-9CDF-41FC-8679-5D12ED38E1C8”.

When I remove
use framework "Foundation"

the script runs fine until I try to write to file.

Any ideas how to use ASObjC with Mail rules?

I wonder if you’re running into the issue outlined here: