Add text without overwriting

I just need a script to copy a email into a text document. so far so easy.
But now I want, that every email who is received next is added to the text document. I got the Mail-rule thing but I just don´t now how to NOT overwrite. I also tried many versions of my little script but I can´t get a solution.


using terms from application "Mail"
	on perform mail action with messages theMessages
		
		tell application "Mail"
			repeat with theMessage in theMessages
				set theText to content of theMessage
				set theFile to (POSIX file "Macintosh HD:Users:*****:Desktop:voting:vote-1.txt")
				set theFile to theFile as string
				open for access theFile
				set fileContents to (read theFile)
				write theText & fileContents to theFile starting at eof
				close access theFile
			end repeat
		end tell
	end perform mail action with messages
end using terms from
tell application "Mail"
	set myMessages to selection
	tell me to perform mail action with messages myMessages
end tell

And sorry for this bad formatting and stuff skills - i´m completly a noob ;D
Hope somebody can help me…

You may try:


using terms from application "Mail"
	on perform mail action with messages theMessages
		set theFile to my buildPath() # Build it only once
		tell application "Mail"
			repeat with theMessage in theMessages
				set theText to content of theMessage
				try
					set fileRef to open for access file theFile with write permission
					write theText to fileRef starting at eof
					close access fileRef
				on error
					close access file theFile
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on buildPath()
	set p2d to path to desktop
	set votingFolder to "voting"
	set targetFolder to (p2d as text) & votingFolder & ":"
	tell application "System Events"
		if not (exists folder targetFolder) then
			make new folder at end of p2d with properties {name:votingFolder}
		end if
	end tell
	set theFile to (targetFolder & "vote-1.txt") # it's a string
	
	return theFile
end buildPath


tell application "Mail"
	set myMessages to selection
	tell me to perform mail action with messages myMessages
end tell

I moved the instruction defining the path of the file out of block speaking to Mail because when it’s in such a block it compiles as :

set theFile to (path to desktop rule type rich text) & "voting:vote-1.txt" # it's a string

which fails if we have to re-compile the code

CAUTION. The script above stores theText at the end of the existing file.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) dimanche 12 mars 2017 09:49:51

You are my HERO!!!

Thanks alot!!!

Man I´m so Happy you made my Week!

Great people on this world ;D

Thanks for the feedback.

Just several years of practice :wink:

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) dimanche 12 mars 2017 15:32:43