How to fix this script .....

I hope to crete a script which allows me to export selected messages to separate text UTF8 files
It should be a a loop telling the script to
1-Create/Select a destination folder
2-open the first selected message in the Apple Mail found messages browser window
3-Rename the mesge with subject and timestamp
4-Save it in the folder of choice as RAW Source (⌥⌘U) in the Apple mail View-> Message-> Raw source combination
5-Repeat with next till last message as resulting from the Selection window?
This is what I did, however nothing get saved while in the Events everythig appear properly


set theOutputFolder to POSIX path of (choose folder)
tell application "Mail"
	set theSelection to selection
	set theMessage to item 1 of theSelection
	repeat with a from 1 to length of theSelection
		set MessagS to source of theMessage
		set MessagID to id of theMessage
		set Mess to MessagID & MessagS
		try
			set theSavePath to theOutputFolder
			save Mess in theSavePath
		end try
		
	end repeat
	
end tell


Thanks for the help

Hi,

I don’t know if Mail.app responds to the save command, but almost every app which contains the save command expects the full HFS path including the file name.

I recommend to use the write command of Standard Additions, for example


set theOutputFolder to (choose folder) as text
tell application "Mail" to set theSelection to selection
repeat with aMessage in theSelection
	tell application "Mail"
		set MessagS to source of aMessage
		set MessagID to (id of aMessage) as rich text
	end tell
	set Mess to MessagID & return & return & MessagS
	
	set theFile to theOutputFolder & MessagID & ".txt"
	try
		set ff to open for access file theFile with write permission
		write Mess to ff as «class utf8»
		close access ff
	on error
		try
			close access file theFile
		end try
	end try
end repeat


Thanks stefan you keep saving me …a