Receiving Error on Write File

I am trying to create a mail rule which appends the body of a message to a file. Ideally this would be a file in iCloud Mobile Documents but for the purpose of debugging, I am trying to do it just do a file in Downloads.

The script is below and the error comes in the write_to_file routine. Hoping for some clues.


tell application "Mail"
	set theSel to selection -- selected emails will be used for test
end tell
using terms from application "Mail"
	perform mail action with messages theSel in mailboxes (missing value) for rule (missing value)
end using terms from

using terms from application "Mail"
	on perform mail action with messages theMessages
		repeat with aMessage in theMessages
			try
				set theSubject to getMessageSubject(aMessage)
				set theBody to getMessageBody(aMessage)
			end try
		end repeat
		-- set thisFile to "Macintosh HD/Users/learned/Library/Mobile Documents/iCloud~co~noteplan~NotePlan/Documents/Calendar/" & theSubject & ".txt"
		set thisFile to "~/Downloads/" & theSubject & ".txt"
		log thisFile
		log theBody
		my write_to_file(theBody, thisFile, true)
	end perform mail action with messages
end using terms from

on getMessageSubject(aMessage)
	tell application "Mail" to return subject of aMessage
end getMessageSubject

on getMessageBody(aMessage)
	tell application "Mail" to return content of aMessage
end getMessageBody

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as string
		log target_file
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file


It’s always helpful if you include the error (verbatim) as well, because the error message is what points you/us to the probable cause.

Skimming your code, however, what jumps out at me is that you’ve prepended the file specifier to your posix path in this line:

open for access file target_file

so try removing that, and just having:

open for access target_file

Secondly, make sure your posix paths are full paths, and don’t use a tilde “~” as a shortcut for the home directory because these will not be expanded by the standard additions commands:

set thisFile to "~/Downloads/" & theSubject & ".txt"

should be:

set thisFile to the POSIX path of (path to downloads folder) & theSubject & ".txt"

tell application "Mail" to set theSel to selection -- selected emails will be used for test

using terms from application "Mail"
	perform mail action with messages theSel in mailboxes (missing value) for rule (missing value)
end using terms from

using terms from application "Mail"
	on perform mail action with messages theMessages
		repeat with aMessage in theMessages
			try
				tell application "Mail"
					set theSubject to subject of aMessage
					set theBody to content of aMessage
				end tell
			end try
		end repeat
		set thisFile to the POSIX path of (path to downloads folder) & theSubject & ".txt"
		log (thisFile as string)
		log theBody
		my write_to_file(theBody, thisFile, true)
	end perform mail action with messages
end using terms from

on write_to_file(this_data, target_file, append_data)
	try
		log (target_file as string)
		set the open_target_file to open for access target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file