Mail.app: Moving messages based on account.

Hi all,

I’m trying to get Mail.app to move messages to specific folders based on their account. I view all my inboxes together in one view, but once a message is “read”, I like to move them to folders based on their accounts. Messages from account A go to “Read mail A”, and all other accounts go to “read mail B”.

A bit of Googling gave me something that takes me halfway there: http://www.aaronsw.com/weblog/001054. All I needed to do was add some code that gets the account of the message, and set the target_mailbox and target_account correctly. So I did this:


using terms from application "Mail"
	on perform mail action with messages the_selection
		if the_selection = {} then
			beep
			return
		end if
		
		tell application "Mail"
			repeat with i from 1 to (count of the_selection)
				set theAccount to the account of the_selection
				display dialog theAccount
				if theAccount is "Marketcom" then
					set target_mailbox to "Read mail"
					set target_account to "Marketcom"
				else
					set target_mailbox to "A read mail folder"
					set target_account to "On My Mac"
				end if
				set mailbox of (item i of the_selection) to mailbox target_mailbox of account target_account
			end repeat
		end tell
	end perform mail action with messages
end using terms from

using terms from application "Mail"
	on run
		tell application "Mail" to set sel to selection
		tell me to perform mail action with messages (sel)
	end run
end using terms from

But (as you’ll see) when I run it, I get a weird error:

“Can’t get account of {message id 139382 of mailbox "INBOX" of account "Emit" of application "Mail"}.”

That doesn’t make sense to me, because the error says it can’t get the account of something that explicitly has an account in it. I must be missing something fundamental here somehow.

Any help would be appreciated. Thanks in advance.

Hi,

One or two things happening here.

You are trying to reference ‘the_selection’ which is a list and not the item ‘i’ of the list.

Also ‘account’ is a property of the ‘mailbox’ and not the message. You should take a look at the Mail.app Applescript dictionary. You can get the account by finding the mailbox and then the account of the mailbox.

Also you need to get the name of the account and coerce it to text to display it.

Compare this to your script:


using terms from application "Mail"
	on perform mail action with messages the_selection
		if the_selection = {} then
			beep
			return
		end if
		
		tell application "Mail"
			repeat with i from 1 to (count of the_selection)
				-- Get the account.
				set theAccount to the account of (mailbox of (item i of the_selection))
				-- Display its name.
				display dialog ((name of theAccount) as text)
				-- Do your action on the message
				(*
				if theAccount is "Marketcom" then
					set target_mailbox to "Read mail"
					set target_account to "Marketcom"
				else
					set target_mailbox to "A read mail folder"
					set target_account to "On My Mac"
				end if
				set mailbox of (item i of the_selection) to mailbox target_mailbox of account target_account
				*)
			end repeat
		end tell
	end perform mail action with messages
end using terms from

using terms from application "Mail"
	on run
		tell application "Mail" to set sel to selection
		tell me to perform mail action with messages (sel)
	end run
end using terms from

Hope this helps.

John M

Hi John,

Thanks so much for that.

Duh. That was just plain stupid on my part. At one point, it was properly referencing item ‘i’ of the list, but I’d been through so many iterations of that script that it was getting sloppier and sloppier.

Aha! That was the bit I was missing. Thank you!

After a bit more mucking around, I discovered that folders on the “top level” of Mail’s hierarchy don’t have an “account”, so I had to make some changes to the way things are assigned in the script. Here’s the completed (and working) script:


using terms from application "Mail"
	on perform mail action with messages the_selection
		if the_selection = {} then
			beep
			return
		end if
		
		tell application "Mail"
			repeat with i from 1 to (count of the_selection)
				set theAccount to the account of (mailbox of (item i of the_selection))
				if (name of theAccount) is "Marketcom" then
					set mailbox of (item i of the_selection) to mailbox "Read Mail" of account "Marketcom"
				else
					set mailbox of (item i of the_selection) to mailbox "A read mail folder"
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from

using terms from application "Mail"
	on run
		tell application "Mail" to set sel to selection
		tell me to perform mail action with messages (sel)
	end run
end using terms from

Thanks again!