Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript to batch-redirect email in Mail.app' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to batch-redirect email in Mail.app
Authored by: ekepenne on Jan 08, '12 04:11:16AM
Hello everybody

Thanks for the tips regarding the forwarding/redirecting of emails.

I'm trying to forward specific incoming emails to my Evernote account. But before I forward the message, I wanted to add the date of the email and the desired Evernote-Notebook and Evernote-Tags to the subject. These added parameters are based on the name of the Mail rule.

Example:
- Initial subject is: This is the subject
- Email is dated: 7 January 2012
- Rule name is: @Notebook1 #Tag1 #Tag2

The new subject of the forwarded email will be:
This is the subject [7 January 2012] @Notebook1 #Tag1 #Tag2

In Evernote the message title will look like this: This is the subject [07.01.2012]
and the message will automatically be moved to Notebook1 and tagged with Tag1 and Tag2.

I used the code listed below and most of it works fine, but I have a strange issue.
The sender and subject are set correctly, but the message body and attachments are duplicated.
If I remove the line set subject of theRedirectedEmail to NewSubject, the message body and attachments is correct.
If I have 2 identical lines with this command set subject of theRedirectedEmail to NewSubject, the message body is tripled. It seems that I get a duplicated body/attachment for each "set" statement I add

Does anyone have any clue why I get this behavior?

Thank you ver much.
Regards, Eric

PS: My Mac runs OSX Lion 10.7.2.


on perform_mail_action(ruleData)
	
	-- Set parameters
	set AppleScript's text item delimiters to ""
	set theRedirectRecipient to "Evernote <MyEmailAddress@m.evernote.com>"
		
	-- Set the Notebook and tags of Evernote based on the name of the rule in Mail
	set NotebookTags to name of |Rule| of ruleData as text
	
	-- Get incoming messages that match the rule
	set selectedMessages to |SelectedMessages| of ruleData
	
	tell application "Mail"
		
		repeat with thisMessage in selectedMessages
			
			-- Get the date the message was sent
			set {year:y, month:m, day:d, hours:h, minutes:min} to thisMessage's date sent
			
			-- Prepare a date stamp " [dd.mm.yyyy] " for further use
			set dateStamp to (" [" & my pad(d) & "." & my pad(m as integer) & "." & y & "] ")
			set NewSubject to subject of thisMessage & dateStamp & NotebookTags
			
			set theRedirectedEmail to redirect thisMessage with opening window
			
			tell theRedirectedEmail
				make new to recipient at beginning of to recipients with properties {address:theRedirectRecipient}
				delete bcc recipients
				delete cc recipients
				set subject of theRedirectedEmail to NewSubject
			end tell
			
			send theRedirectedEmail
			
		end repeat
		
	end tell
	
end perform_mail_action

-- Adds leading zeros to date components
on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad


[ Reply to This | # ]