AppleScript Programming/Mail.app archive selected inbox messages

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

This script tells Mail.app to move the selected mails to a certain directory/mailbox. Works fine with GMail.

tell application "Mail"
	set selectedMails to the selection
	if the length of selectedMails is greater than 0 then
		repeat with theMessage in selectedMails
			if the name of the mailbox of theMessage is "INBOX" then
				# Set "gmail" to whatever your account is called in Mail.app's preferences
				if the account of the mailbox of theMessage is account "gmail" then
					# Note: instead of "All Mail" below, I had to use "[Gmail]/All Mail" -Simon, using Mail 3.5 (930.3) & Gmail w/ IMAP
					move theMessage to mailbox "All Mail" in account "gmail"
				# Add custom accounts with custom names and custom archive mailboxes here
				#else if the account of the mailbox of theMessage is account "uni" then
				#	move theMessage to mailbox "Archive" in account "uni"
				else
					say "Your mail didn't belong to any account I know."
				end if
			else
				say "Only mails in inbox can be moved"
			end if
		end repeat
	else
		say "You have to select mails first"
	end if
end tell