Getting body of message from Mail.app

Hi All,

I have created a rule in Mail.app that is triggered by a certain subject line. The action it performs is to activate an Applescript.

How can I get the contents of the message that triggered the rule to be passed as text to a variable in the Applescript that the rule activates?

Thanks a bunch,
Mark

Hi Mark,

here a sample script, which writes the contents of the mail(s)
into a text file on desktop

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set theSender to (extract name from sender)
					set theDate to date received
					set theContents to content as text
				end tell
			end tell
			set theDate to short date string of theDate
			set TempFile to ((path to desktop) as string) & theSender & "_" & theDate
			set theTextFile to open for access TempFile with write permission
			write theContents to theTextFile
			close access theTextFile
		end repeat
	end perform mail action with messages
end using terms from

Hi Stefan,

Thanks for the reply. It works just as it should but I can’t seem the get theContents to be passed to another variable called the_data on which I need to perform other functions that I’ve already scripted. I’ve tried placing your script at the top of my current script and moved the lines

	end repeat
end perform mail action with messages

end using terms from
to the very end but this doesn’t seem to work.

Here is the script as I’m trying to use it.


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set theSender to (extract name from sender)
					set theDate to date received
					set theContents to content as text
				end tell
			end tell
			set theDate to short date string of theDate
			set TempFile to ((path to desktop) as string) & theSender & "_" & theDate
			set theTextFile to open for access TempFile with write permission
			write theContents to theTextFile
			close access theTextFile
			

				set the_data to theContents as text
				
				set ASTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "**"
				
				set the_data to text items 2 through -2 of the_data as string
				
				tell application "TextWrangler"
					sort lines of the_data
				end tell
				set the_data to result
				
				set dataSets to {}
				
				set AppleScript's text item delimiters to "
"
				set dataSets to every text item of the_data
				
				repeat with i from 1 to count of dataSets
					set newList to text items 2 through ((count items of dataSets) - 1) of dataSets
				end repeat
				set orderCount to count items of newList
				
				set {clientName, jobNumber, imageName} to {{}, {}, {}}
				set AppleScript's text item delimiters to "/"
				repeat with i in newList
					set end of clientName to text item 1 of i
					set end of jobNumber to text item 2 of i
					set end of imageName to text item 3 of i
				end repeat
				
				set AppleScript's text item delimiters to "Job_"
				repeat with i from 1 to count of jobNumber
					set item i of jobNumber to text item 2 of item i of jobNumber
				end repeat
				
				set AppleScript's text item delimiters to ".jpg"
				repeat with i from 1 to count of imageName
					set item i of imageName to text item 1 of item i of imageName
				end repeat
				
				set AppleScript's text item delimiters to ASTID
				
				set jobName to ""
				set jobName to ((item 1 of jobNumber) & " " & (item 1 of clientName))
				
				set folderPath to ("Central Storage:Work Storage:" & jobName)
				set sessionPath to (folderPath & ":" & jobName & ".session")
				set folderStatus to ""

				tell application "Finder"
					if exists folder (folderPath) then
						tell application "Capture One PRO"
							activate
							open (sessionPath)
						end tell
					else
						display dialog "The folder for Job " & (text item 1 of jobNumber) & " could not be found at " & return & folderPath
					end if
				end tell
			end using terms from
		end repeat
	end perform mail action with messages
end using terms from
--return {newList, orderCount, clientName, jobNumber, imageName, jobName, folderPath, sessionPath}

The emails that trigger this action will always look something like

My script extracts the file paths between the “**” then sorts them and creates some other variables used to determine which job to open with Capture One Pro. How can I get theContents into the variable the_data?

UPDATE:
It looks as if it is now working. I think the problem was actually a stray carriage return in the formatting of the message sent from the cgi app. I believe the stray return was causing the script to grab “” on the line

set the_data to text items 2 through -2 of the_data as string

I removed the stray return sent another message from the cgi app and Mail received the message triggered the script and performed as it should. Thanks again for your help!:smiley:

Thanks a bunch,
Mark