How to write variable content to file?

Tried this code, but the content of the variable “theMessage” is not written to the file. Anyone know why?

global theMessge
tell application “Mail”
selection
set theSelection to selection
set theMessage to item 1 of theSelection
content of theMessage
end tell
set filepath to “/Users/leo/Desktop/foo.txt”

try
set openfile to open for access (POSIX file filepath) with write permission
write theMessage to openfile
close access openfile
on error
try
close access openfile
end try
end try

There were a few things that were wrong about your script.

  1. The variable theMessage doesn’t need to be “globified.”
  2. You set variable theMessage to data about the message, but not the content of the message itself.
set theMessage to item 1 of theSelection

instead of

set theMessage to content of item 1 of theSelection
  1. You used “(POSIX file filepath)” instead of just using “filepath.”

Here is a working script:

tell application "Mail"
	set theSelection to selection
	set theMessage to content of item 1 of theSelection
end tell
set filepath to (path to desktop as text) & "foo.txt"
try
	set openfile to open for access filepath with write permission
	write theMessage to openfile
	close access openfile
on error
	try
		close access openfile
	end try
end try

Hi Dylan,

according to Scripting Additions’ dictionary it’s recommended to use a file specifier or an alias as parameter of the read command. And in the error branch you should close the file, not the reference number


tell application "Mail"
	set theSelection to selection
	set theMessage to content of item 1 of theSelection
end tell
set filepath to (path to desktop as text) & "foo.txt"
try
	set openfile to open for access file filepath with write permission
	write theMessage to openfile
	close access openfile
on error
	try
		close access file filepath
	end try
end try


Hello.

I have a version that takes height for that several instances of the script may be running under the current application If you then use the filename, you may close another instance of the open file.
This one does not close the file unless an actual file handler were returned -which I doubt open for access will if there was an error never the less, if there are some lower level structures still open, I leave it to the script to close its resources upon exit.


tell application "Mail"
   	set theSelection to selection
   	set theMessage to content of item 1 of theSelection
end tell
set filepath to (path to desktop as text) & "foo.txt"
try
	set openfile to open for access file filepath with write permission
on error ” I think this really could be condensed to a single return ” the way i see it
	try
		set n to openfile
		close access openfile
		return
	on error -- The file was never opened 
		return
	end try
end try
try
	write theMessage to openfile
	close access openfile
on error
	try
		close access openfile
	end try
end try


thanks all.

appreciate the file i/o information as well but the main problem was described:

  1. You set variable theMessage to data about the message, but not the content of the message itself.

I guess I got carried away. :stuck_out_tongue:

Sure did not mind the file i/o as I have only just started looking at Applescript though I have used Macs since '84. I’ve much to learn, thanks!