Move All Mail Messages that are unread

I have really been looking to take control of my email and have been looking at the best ways to handle this. After going through all of the filters, and going through the possibility of using Act-on, I have come to the conclusion of what I am trying to do isn’t very common.

Here is my use case.

I find that I only click on messages that I want to read. I get emails from 3rd parties, digests for user groups, job offerings. And, while some time I read them, most of the time, I just leave the marked as unread.

What I would like to do, and I did some searching here and couldn’t find what I was looking for, is to write a script that will on command look through all of my messages that are older than 24 hours. Move the Unread messages to the garbarge, and move the read messages to another folder.

Any ideas on how to accomplish such a task would be greatly appreciated.

Hope you all are having a great weekend. Thank you for your time.

Nobody has any ideas? Tears.

Hi, aidanshealy. Welcome to MacScripter.

I don’t use Mail myself, but this works in a brief trial with a few test messages:

set |24h ago| to (current date) - days

tell application "Mail"
	delete (messages of inbox whose read status is false and date received comes before |24h ago|)
	move (messages of inbox whose date received comes before |24h ago|) to mailbox "Read Mail" -- . or whatever your destination folder's called.
end tell

Thanks for the reply, the script works perfect on all my inboxes.

The next question would be how do I run it on a specific inbox. I tried making some modifications to the script. Here is what I have. However, I keep getting back to the error. Cannot find the mailbox.


set _ago to (current date) - days
set _inbox to "Inbox:Gmail"

tell application "Mail"
	move (messages of _inbox whose read status is false and date received comes before _ago) to mailbox "Gmail - Unread"
	move (messages of _inbox whose date received comes before _ago) to mailbox "Gmail - Archive"
end tell

In my script, ‘inbox’ is the Mail keyword for the Inbox folder. In yours, ‘_inbox’ is a variable containing the text “Inbox:Gmail”. If you want it to refer to a Mail folder, you have to put ‘mailbox’ in front of it as with the destination folders.

I presume “Inbox:Gmail” is the folder’s name and isn’t meant to refer to a folder called “Gmail” in the Inbox, as I can’t see how to create such a thing.

I have an account that is labelled Gmail, when I do a list of mailbox names. That is the name that shows. That is why I was trying to call that direct Folder.

I have four accounts in my Inbox, I only want to process one of those four accounts with the script.

I tried calling the Mailbox many different ways, and none of them worked. I keep getting end of line errors.

I tried…


set _inbox to mailbox "Gmail"
set _inbox to mailbox ("Gmail")
set _inbox to mailbox "Inbox/Gmail"
set _inbox to mailbox Gmail
set _inbox to mailbox mailaddress@gmail.com

I cannot seem to be able to find what the right syntax is for finding the correct account / mailbox inside of the Inbox structure.

OK. By creating an additional account in Mail with the description “Gmail”, I’ve succeeded in getting two folders to appear in the inbox: one with the name of the exisiting account and one called “Gmail”.

A bit of exploratory scripting reveals that the reference to the “Gmail” folder is not ‘mailbox “Gmail” of inbox’, but ‘mailbox “INBOX” of account “Gmail”’! The keyword ‘inbox’ then becomes a generic term for the inboxes of both accounts.

To set a variable to a mailbox, you have to do it within the ‘tell application “Mail”’ statement, because Mail knows what a mailbox is and vanilla AppleScript doesn’t. It also turns out that trying to move messages which don’t exist causes an error, so that needs to be checked:

set _ago to (current date) - days

tell application "Mail"
	set _inbox to mailbox "INBOX" of account "Gmail"
	
	set oldUnreadMessages to messages of _inbox whose read status is false and date received comes before _ago
	if ((count oldUnreadMessages) > 0) then move oldUnreadMessages to mailbox "Gmail - Unread"
	
	set oldReadMessages to messages of _inbox whose date received comes before _ago
	if ((count oldReadMessages) > 0) then move oldReadMessages to mailbox "Gmail - Archive"
end tell

If you particularly want to set a variable to “Gmail” somewhere near the top of the script, you can of course do something like this:

set _ago to (current date) - days
set _inbox to "Gmail"

tell application "Mail"
	set oldUnreadMessages to messages of mailbox "INBOX" of account _inbox whose read status is false and date received comes before _ago
	if ((count oldUnreadMessages) > 0) then move oldUnreadMessages to mailbox "Gmail - Unread"
	
	set oldReadMessages to messages of mailbox "INBOX" of account _inbox whose date received comes before _ago
	if ((count oldReadMessages) > 0) then move oldReadMessages to mailbox "Gmail - Archive"
end tell

It looks like it is starting to get close… However, I got a bunch of errors.

Mail got an error: Can’t make {message id 794587 of mailbox “INBOX” of account “Gmail”, message id 794585 of mailbox “INBOX” of account “Gmail”, message id 794583 of mailbox “INBOX” of account “Gmail”, message id 794581 of mailbox “INBOX” of account “Gmail”, message id 794572 of mailbox “INBOX” of account “Gmail”, message id 794567 of mailbox “INBOX” of account “Gmail”, message id 794564 of mailbox “INBOX” of account “Gmail”, message id 794559 of mailbox “INBOX” of account “Gmail”, message id 794554 of mailbox “INBOX” of account “Gmail”, message id 794553 of mailbox “INBOX” of account “Gmail”, message id 794551 of mailbox “INBOX” of account “Gmail”, message id 794401 of mailbox “INBOX” of account “Gmail”, message id 794398 of mailbox “INBOX” of account “Gmail”, message id 794392 of mailbox “INBOX” of account “Gmail”, message id 794367 of mailbox “INBOX” of account “Gmail”, message id 798613 of mailbox “INBOX” of account “Gmail”, message id 798614 of mailbox “INBOX” of account “Gmail”, message id 798615 of mailbox “INBOX” of account “Gmail”, message id 798637 of mailbox “INBOX” of account “Gmail”, message id 798638 of mailbox “INBOX” of account “Gmail”, message id 798616 of mailbox “INBOX” of account “Gmail”, message id 798599 of mailbox “INBOX” of account “Gmail”, message id 798617 of mailbox “INBOX” of account “Gmail”, message id 798633 of mailbox “INBOX” of account “Gmail”, message id 798618 of mailbox “INBOX” of account “Gmail”, message id 798619 of mailbox “INBOX” of account “Gmail”, message id 798610 of mailbox “INBOX” of account “Gmail”, message id 798620 of mailbox “INBOX” of account “Gmail”, message id 798629 of mailbox “INBOX” of account “Gmail”, message id 798594 of mailbox “INBOX” of account “Gmail”, message id 798630 of mailbox “INBOX” of account “Gmail”, message id 798631 of mailbox “INBOX” of account “Gmail”, message id 798640 of mailbox “INBOX” of account “Gmail”, message id 798634 of mailbox “INBOX” of account “Gmail”, message id 798635 of mailbox “INBOX” of account “Gmail”, message id 798636 of mailbox “INBOX” of account “Gmail”, message id 798621 of mailbox “INBOX” of account “Gmail”, message id 798622 of mailbox “INBOX” of account “Gmail”, message id 798592 of mailbox “INBOX” of account “Gmail”, message id 798602 of mailbox “INBOX” of account “Gmail”, message id 798603 of mailbox “INBOX” of account “Gmail”, message id 798604 of mailbox “INBOX” of account “Gmail”, message id 798601 of mailbox “INBOX” of account “Gmail”, message id 798598 of mailbox “INBOX” of account “Gmail”, message id 798632 of mailbox “INBOX” of account “Gmail”, message id 798623 of mailbox “INBOX” of account “Gmail”, message id 798606 of mailbox “INBOX” of account “Gmail”, message id 798624 of mailbox “INBOX” of account “Gmail”, message id 798609 of mailbox “INBOX” of account “Gmail”, message id 798607 of mailbox “INBOX” of account “Gmail”, message id 798608 of mailbox “INBOX” of account “Gmail”, message id 798605 of mailbox “INBOX” of account “Gmail”, message id 798625 of mailbox “INBOX” of account “Gmail”, message id 798626 of mailbox “INBOX” of account “Gmail”, message id 798627 of mailbox “INBOX” of account “Gmail”, message id 798639 of mailbox “INBOX” of account “Gmail”, message id 798600 of mailbox “INBOX” of account “Gmail”, message id 798628 of mailbox “INBOX” of account “Gmail”, message id 798611 of mailbox “INBOX” of account “Gmail”, message id 798612 of mailbox “INBOX” of account “Gmail”, message id 798597 of mailbox “INBOX” of account “Gmail”, message id 798641 of mailbox “INBOX” of account “Gmail”, message id 798596 of mailbox “INBOX” of account “Gmail”, message id 798595 of mailbox “INBOX” of account “Gmail”} into type specifier.

I have no idea what these are coming from.

Hmm. It looks as if your version of Mail doesn’t like to have a list as the direct object of ‘move’, although it works for me with Mail 4.5 in Snow Leopard. Does it work for you to use the references?

set _ago to (current date) - days

tell application "Mail"
	set _inbox to mailbox "INBOX" of account "Gmail"
	
	set oldUnreadMessages to a reference to (messages of _inbox whose read status is false and date received comes before _ago)
	if ((count oldUnreadMessages) > 0) then move oldUnreadMessages to mailbox "Gmail - Unread"
	
	set oldReadMessages to a reference to (messages of _inbox whose date received comes before _ago)
	if ((count oldReadMessages) > 0) then move oldReadMessages to mailbox "Gmail - Archive"
end tell

Victory! You are my hero of the day. Thank you so much. This script is going to help me tremendously.