mipmip
6/27/2012 - 10:37 PM

Auto create mail rules

Auto create mail rules

-- TODO fix make move to mailbox rule
-- TODO set condition 

tell application "Mail"
	
	set theseMessages to (get selection)
	repeat with thisMessage in theseMessages
		set theSender to sender of thisMessage
		
		set SenderName to extract name from sender of thisMessage
		set theAddress to extract address from sender of thisMessage
		
		
		(*		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "@"
		set addresName to text item 1 of theDomain
		set theDomain to text item 2 of theDomain
		set AppleScript's text item delimiters to "."
		set theDomain to text item 1 of theDomain
		set AppleScript's text item delimiters to tid
*)
		set newMailboxName to "from " & theAddress
		
		if (mailbox newMailboxName exists) = false then
			make new mailbox with properties {name:"autoRuleFolders/" & newMailboxName}
			--move aMessage to mailbox theArchiveMailboxName		
		end if
		
		set newRule to make new rule at end of rules with properties {name:"autoRule:" & theAddress, enabled:true}
		--, move message:newMailboxName}
		tell newRule
			--make new rule condition at end of rule conditions with properties {rule type:header sender, qualifier:does contain value, expression:theAddress}
		end tell
		
	end repeat
	
end tell