Reference a message in Mail from its path

I am looking for a way to speed up a Mail search.

I can reference a list of messages with:

set findAddress to ""
tell application "Mail" to set myMessages to every message of inbox whose sender contains findAddress

However, if the message is not located in the inbox I need to search everywhere else with something along the lines of:

set findAddress to ""
tell application "Mail" to set myMessages to every message of every mailbox of every account whose sender contains findAddress

The script above will take too long so lets use mdfind instead:

set findAddress to ""
set theEmails to paragraphs of (do shell script "mdfind -onlyin ~/Library/Mail \"kMDItemAuthorEmailAddresses == '" & findAddress & "' || kMDItemRecipientEmailAddresses == '" & findAddress & "'\"")

That is much faster but now I have paths to the messages themselves bringing us to my question… Is there a way to reference messages in Mail from the paths to the message?

tell application “Finder” – or Mail, either will do it.
open POSIX file (“/Users/bell/Library/Mail/Mailboxes/Exch.mbox/Messages/77326.emlx”)
end tell

Thanks Adam.

Ultimately I would like to delete these messages. I don’t want hundreds of windows flashing before the action is performed on each message.


You should really be able to send the result from the mdfind query over to xargs in a pipe, that then will delete the files. I think mdfind −0 puts a null byte after each item it has found. xargs can be made to handle this with the −0 option specified.

Thanks McUsr.

I have already tried that but since I am using an IMAP account, the messages are restored when Mail is launched again.


You can always move the message on your disk to an archieve, and delete the message from the IMAP account. Or move the message to some folder on your IMAP account, to get it out of the way.
You can even forward the message to another account, that you don’t load, to keep an archieve, before you delete it.

I’m not sure I follow you. When I delete the message from the library, quit Mail, then re-launch it … the message is restored to its original location because I did not delete it from the Mail program.


I meant that you should first archieve the message on the IMAP server, either into a folder, or forward the message to another account.

Then delete the message from the IMAP server.

Then delete the downloaded message from the disk.

Now, I haven’t tried this really, :slight_smile: and now, if mail synchronizes your disk with the IMAP server, then you are in bad luck, until you reconfigure your IMAP server. :slight_smile:

It should work by taking a backup of the message, delete from IMAP server, and delete from disk.

If that doesn’t work, then you must figure out something else for that account, maybe something that doesn’t involve Apple Mail.


If you start from finding the message by mdfind, then there should be some way of extracting the message id from the emlx file, so that you can get at it from mail, and take the whole delete process from there by using the message id.


That is why it is is wise to delete the message from the mail server first, then delete it from the hard disk.