Help using Applescript to delete messages in Mail.app

I have two Gmail accounts (one actual Gmail account, and one Google Apps account), and deleting messages from the Inbox is a hassle that seems solvable with a script.

Basically, what I want is a script that would identify what account the currently selected message(s) belongs to, then move the message to the corresponding accountName > [Gmail] > Trash folder (I think that's the path).

Then I could set that up with either Fast Scripts or Quicksilver to quickly run the script to trash the messages, rather than have to expand the necessary folders to drag the message to the trash.

My Applescript-fu is still weak. I've poked around in the Mail.app dictionary, and this seems possible, but actually writing the script is out of reach for me. Can someone help me out?

Thanks in advance!

MacBook 2.0ghz, Mac OS X (10.5.7)

Posted on Jun 16, 2009 12:05 AM

Reply
6 replies

Jun 16, 2009 10:45 PM in response to matthewloewen

If I wanted to filter the messages to go into the account specific Trash mailbox, is that a trivial process? Could I:


tell application "Mail"
set theMessages to (get selection)
set theAccount to (get account of theMessages)
move theMessages to mailbox "Trash" of "Gmail" of account "theAccount"
end tell


Close, but not quite.

The issue is that get selection will return a list of messages. Even if only one message is selected you'll still get a list - just a list containing one item.

My original code would work because Mail can move a list of messages in one go. However, in your example you're trying to extract a property from the messages, but you can't extract the 'account' of a list. Especially when you consider that you may have multiple messages from multiple accounts selected - which one should it take?

The issue is compounded slightly by the fact that there is no 'account' property for a message. You have to get that indirectly via the message's mailbox property.

Fortunately the solution is simple - just iterate through the messages:

tell application "Mail"
set theMessages to (get selection)
repeat with eachMessage in theMessages
set theAccount to account of (mailbox of eachMessage)
move eachMessage to mailbox "Trash" of theAccount
end repeat
end tell

Jun 16, 2009 7:55 AM in response to matthewloewen

Several points come to mind...

Basically, what I want is a script that would identify what account the currently selected message(s) belongs to, then move the message to the corresponding accountName > Gmail > Trash folder (I think that's the path).


Do you need to move the messages to the appropriate trash folder? I don't know about GMail, but typically you can just delete the message and Mail.app takes care of the move. At least that's how it works on my (non-GMail) accounts.

rather than have to expand the necessary folders to drag the message to the trash


Even without Mail.app doing the sorting, there's no need to 'expand the folders' nor 'drag the message':

From what I can see, the following should do exactly what you want:

tell application "Mail.app"
set theMessages to (get selection)
delete theMessages
end tell

Jun 16, 2009 4:29 PM in response to Camelot

Camelot wrote:
Do you need to move the messages to the appropriate trash folder? I don't know about GMail, but typically you can just delete the message and Mail.app takes care of the move. At least that's how it works on my (non-GMail) accounts.

rather than have to expand the necessary folders to drag the message to the trash


Even without Mail.app doing the sorting, there's no need to 'expand the folders' nor 'drag the message':

From what I can see, the following should do exactly what you want:

tell application "Mail.app"
set theMessages to (get selection)
delete theMessages
end tell


Thanks for your reply.

Gmail works in a slightly different way from most IMAP setups. With the recommended Gmail setup, when you "delete" a message from the Inbox, it doesn't get deleted. Instead, it gets "archived." So, when you delete a message from the Inbox, it gets stored in a special IMAP folder called "All Mail."

To actually delete a message in Gmail's IMAP, you need to move the message to a special folder called "Trash."

Thus, each of my two Gmail accounts has a special trash folder. For tidiness, I thought it would be best to move the deleted messages to the appropriate Trash folder, but I suppose that an easier route would be to file all trashed messages in one of the two special Trash folders. That too would be best served by a script, because dragging the messages to a special folder is clumsy at best.

Jun 16, 2009 6:29 PM in response to matthewloewen

I'm still surprised if the above doesn't work - what you've said doesn't sound much different from any IMAP server, other than the mailbox name, maybe.

That said, if what you want to do is move a message to another mailbox then try:

tell application "Mail"
set theMessages to (get selection)
move theMessages to mailbox "Trash" of account "Gmail"
end tell


The 'of account...' may or may not be needed.

Jun 16, 2009 7:23 PM in response to Camelot

I know it sounds very similar, but if you want Gmail to function in Mail.app like it does in Gmail, where you archive most but not all of your mail, there are some unique settings.

Gmail's recommended settings for Mail.app:

(note: for the rest of this message, if Gmail is a link, that's because I can't figure out how to not make hard-brackets not evoke some kind of special message board code.)

Trash:
Move deleted messages to the Trash mailbox > do NOT check
Store deleted messages on the server > do NOT check

* Do NOT save deleted messages on the server. Messages that are deleted from an IMAP folder (except for those in [Gmail]/Spam or [Gmail]/Trash) only have that label removed and still exist in All Mail. Hence, your client doesn't need to store an extra copy of a deleted message.

* Do NOT save deleted messages to your [Gmail]/Trash folder because this will delete a message in all folders.

* Do NOT save deleted messages to your [Gmail]/All Mail folder as some clients will try to empty this folder and ultimately fail. This can lead to delayed mail access or excessive battery consumption on a mobile device.


I can't try your script just now (at work), but I will try it once I get home. It looks like it should work in the most basic scenario.

If I wanted to filter the messages to go into the account specific Trash mailbox, is that a trivial process? Could I:

tell application "Mail"
set theMessages to (get selection)
set theAccount to (get account of theMessages)
move theMessages to mailbox "Trash" of "[Gmail]" of account "theAccount"
end tell


?

That's just my guess at what the Applescript might look like.

Thanks for all your help.

Jun 17, 2009 5:58 AM in response to Camelot

I had to change the "Trash" reference to "Gmail (enclosed in hard brackets)/Trash" to get it to file the message in the actual Trash instead of an IMAP folder called Trash, but apart from that, it works perfectly! I've tried deleting messages both from the general Inbox and from the account specific inboxes, and both work as expected.

This is a problem that's been bugging me since Gmail first allowed IMAP. Thank you so much for taking the time to help me out!

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.

Help using Applescript to delete messages in Mail.app

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