Help with aging a particular mailbox in Mail.app

Hey everyone. I’m trying to write a script that will move my listserv messages into a separate Mail.app mailbox and then delete ones that are older than ‘x’ days. I spent 6 hours yesterday trying to write the bloody thing instead of doing real work. It doesn’t really work and it’s really inefficient.

Here it is:


tell application "Mail"
	set myList to {}
	repeat with oneAccount in (get imap accounts)
		set myMailbox to name of mailboxes of oneAccount
		set myList to (myList & myMailbox)
	end repeat
end tell

set TargetMailbox to {choose from list myList}

set prompt to "How long do you want to keep"
set question to display dialog prompt ¬
	buttons {"Days", "Weeks", "Months"} default button 2 ¬
	default answer "2"

set dNumber to text returned of question as integer
set dtext to button returned of question
set dtype to words of dtext

if dtext is "days" then set dshift to ((current date) - dNumber * days)
if dtext is "Weeks" then set dshift to ((current date) - dNumber * weeks)
if dtext is "Months" then set dshift to ((current date) - dNumber * months)

tell application "Mail"
	repeat with oneAccount in (get imap accounts)
		repeat with oneMailbox in mailboxes of oneAccount
			if name of oneMailbox is TargetMailbox then
				set theMessages to messages of oneMailbox
				repeat with theEmail in theMessages
					if date received of theEmail is less than dshift then
						set background color of theEmail to green
						delete theEmail
					end if
				end repeat
			end if
		end repeat
	end repeat
end tell

I know it’s terrible, but the more I tried to tinker with it the more it broke and I had the damnedest time trying to select all messages from a specific folder/mailbox that’s not the Inbox, Outbox, Junk, or Trash.

On a side note, I’d like to know how to turn the text of a variable into inline code so that the text returned could represent a class or command or the like.

Oh and I’m working in Leopard.

Help.

Hi,

you should first ask for the account and then for the mailbox to specify the mailbox
Try something like this:


tell application "Mail" to set myaccounts to name of imap accounts
set TargetAccount to choose from list myaccounts with prompt "Choose IMAP account"
if TargetAccount is false then return
set TargetAccount to item 1 of TargetAccount
tell application "Mail" to set myboxes to name of mailboxes of imap account TargetAccount
set TargetMailbox to choose from list myboxes with prompt "Choose mailbox of " & TargetAccount
if TargetMailbox is false then return
set TargetMailbox to item 1 of TargetMailbox

set prompt to "How long do you want to keep"
set question to display dialog prompt ¬
	buttons {"Days", "Weeks", "Months"} default button 2 ¬
	default answer "2"
try
	set dNumber to text returned of question as integer
	set dtext to button returned of question
		
	if dtext is "days" then
		set dshift to ((current date) - dNumber * days)
	else if dtext is "Weeks" then
		set dshift to ((current date) - dNumber * weeks)
	else if dtext is "Months" then
		set dshift to ((current date) - dNumber * months)
	end if
	
	tell application "Mail"
		repeat with theEmail in (get messages of mailbox TargetMailbox of account TargetAccount)
			if date received of theEmail is less than dshift then
				set background color of theEmail to green
				delete theEmail
			end if
		end repeat
	end tell
on error
	display dialog "Error: Enter a number"
end try