Mail roul script for message export

Hello everyone

I’ve got a problem. I would like to make an applescript and I’m a real beginner.
I would like to make a script witch i cane put in a mail role.
Now every time when a mail with the subject FormToEmail comes in the script should run.
This script export the mail to a txt file.
I wrote a script but now I’ve got an other Problem. The download from the Mail goes to long and now the script is running before the Mail is loaded. And now the Script is running with the last message before the new came in.


tell application "Mail"
	activate
	set auswahl to first message of inbox
	tell auswahl
		set teilnehmerL to address of to recipients
		set optTeilnehmerL to address of cc recipients
		set {kopf, txt} to {subject, content as text}
		if txt starts with "?" then set txt to source as text
	end tell
end tell


set to_day to short date string of (current date)
set to_hours to hours of (current date)
set to_minutes to minutes of (current date)
set to_seconds to seconds of (current date)

set text item delimiters of AppleScript to return
set dateiId to open for access file ((path to startup disk as text) & ":lightroom_collection:" & to_day & "_" & to_hours & to_minutes & to_seconds & ".txt") with write permission
set eof of dateiId to 0
write ({kopf, txt} as text) to dateiId
close access dateiId

thx for help
best regards number1

Hi number1,

die Namen der Variablen hören sich ziemlich deutsch an :wink:

to use a script as a mail rule it must be wrapped with the perform mail action event handler and triggered by a specified rule

try this


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set startupDisk to path to startup disk
		set destinationFolder to (startupDisk as text) & "lightroom_collection:"
		repeat with oneMessage in theMessages
			-- 	set teilnehmerL to address of to recipients
			--	set optTeilnehmerL to address of cc recipients
			tell oneMessage to set {kopf, txt} to {subject, its content}
			if txt starts with "?" then set txt to source of oneMessage
			set targetFile to destinationFolder & (do shell script "/bin/date +%d.%m.%y_%H%M%S" & ".txt")
			try
				set dateiId to open for access file targetFile with write permission
				set eof of dateiId to 0
				write kopf & return & txt to dateiId
				close access dateiId
			on error
				try
					close file targetFile
				end try
			end try
		end repeat
	end perform mail action with messages
end using terms from

I commented out the non used variables teilnehmerL and optTeilnehmerL