How can I use Applescript to access Mail Messages directly?

I want to step through messages, generate a list of the form:


UniqueID - {properties}


Where the properties contain some combination of sender information, receipt date, and keywords.


The idea is to process the list based on the properties externally to Applescript and Mail using something a LOT faster than Applescript.


Once the result for each message is known (delete, move, change flag, archive or do nothing) I want to access each message and apply the result.


The problem arises in being able to reference the messages. There's an "id" for each message, and a "Message Id", but it's not as simple as telling Applescript to "delete Message whose id is 67678" or "delete message whose message id is "00000000000046c6d305cfd2b3b4@google.com".


Indexing by the number of elements in a mailbox


("message 6 of mailbox "INBOX" of someaccount") 


doesn't work because new messages and deletions could be received throwing off the count.


Iterating through all the messages is a poor approach because it's an O(n^2) algorithm (for every message, you have to scan the entire list) and Applescript is slow. So if you are trying to update 1000 messages, each message update requires, on average 500 searches. That's ~500 times slower than an algorithm that allows direct access to an email message.


Somewhere there should be an INDEX of all messages (which I would expect to be based on something like "Message Id", which appears to be a unique identifier. Access should be as simple as:


tell application "Mail"
   delete the message whose message id is "535454fdded@google.com"
end

But I can't get that to work.


Any ideas how to access the mail messages directly?

Posted on Feb 24, 2024 9:51 AM

Reply

Similar questions

There are no replies.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I use Applescript to access Mail Messages directly?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.