Having problems with making a file and storing the alias

I am trying to save selected emails to sequential text files. If anyone can look at this, I would greatly appreciate it. The problem is that I can create the new file but do no know how to put the alias in a variable so I can output the text to it. This is what I have so far:


on perform_mail_action(info)
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of info
		
		set MessageNumber to 0
		repeat with eachMessage in selectedMessages
			set MessageNumber to MessageNumber + 1
			set outputfilename to "Message" & (MessageNumber as string) & ".txt"
			tell application "Finder"
				make new file at desktop with properties {name:outputfilename}
				make new alias file to outputfile at desktop with properties {name:outputfilename}
			end tell
			--set outputfile to alias outputfilename
			open for access outputfile with write permission
			set eof of outputfile to 0
			set theEmail to content of eachMessage as text
			write theEmail to outputfile
			close access outputfile
		end repeat
		
	end tell
end perform_mail_action

Any help would be greatly appreciated.

Carey

Does this help?

tell application "Finder" to ¬
	make new file at desktop with properties {name:outputFilename}
set outputFile to result as alias

Also, FWIW, the Standard Additions dictionary says Open For Access will create a new file if the specified one doesn’t exist.

What OS are you running?

10.4.1

However, I got it to work with this script:

on perform_mail_action(info)
	set selectedMessages to |SelectedMessages| of info
	tell application "Finder"
		set the previousItems to items of the desktop whose name contains "Message"
		delete every item of previousItems
	end tell
	repeat with a from 1 to length of selectedMessages
		set currentMessage to item a of selectedMessages
		tell application "Mail"
			set theBody to content of currentMessage as string
			if theBody begins with "?" then set theBody to source of currentMessage as string
		end tell
		set outputfilename to "Message" & a & ".txt"
		set outputFilePath to (path to desktop folder as string) & outputfilename
		try
			close access file outputFilePath
		end try
		open for access file outputFilePath with write permission
		set eof of file outputFilePath to 1
		write theBody to file outputFilePath starting at eof
		close access file outputFilePath
	end repeat
end perform_mail_action

My next question is how can I run this script without using a rule? I tried putting the script in the Mail Scripts folder, but when I click on it, it doesn’t execute.

Carey