Selected Mail Message to Text File (formatted for iPod notes)

I’ve been working away at this script for about a week, and have finally run myself up against a wall, which figures, since the dang thing is very close to being done.

Anyway, the intention is to grab content, subject, date sent, and sender from the selected Mail message and using that data create a new text file (or files) which are formatted for viewing on the iPod. Eventually I will be adding the HTML in order to link the various pages of messages which are larger than 4kb (iPod note limit). Not there yet, but close.

The problem is that I can’t get TextEdit to create the text file. I’m using a peice of code for this procedure which has worked befoe in a different context; as far as I can tell I’ve adapted it properly, but for some reason it just doesn’t work. No file is created.

Also, a note regarding my skill-level: I’ve been playing with AppleScript for a couple of years on-and-off, and I think I’m somewhat familiar with most of the basic concepts. My competence-level should be apparent from the examples below. I know the “Mail” portion of the code isn’t as clean as it could be, but it works. I’ll clean it up when I’ve got the script running as desired.

Here’s the offending script. It’s the TextEdit portion at the bottom (the part which is supposed to write the file) which is giving me trouble.

set text item delimiters to ", "

tell application "Finder"
	set message_folder to folder "Selected Mail Messages" of folder "Preferences" of folder "Library" of home exists
	if message_folder exists then
		--does nothing
	else
		set new_folder to make new folder in folder "Preferences" of folder "Library" of home
		try
			set name of new_folder to "Selected Mail Messages"
		end try
	end if
	set message_folder to folder "Selected Mail Messages" of folder "Preferences" of folder "Library" of home
	set message_notes to every item of message_folder
	set note_count to count of message_notes
end tell

tell application "Mail"
	set message_text to {}
	set selected_messages to selection
	if (count of selected_messages) is 0 then display dialog "Please select a message in Mail for export." buttons {"Cancel"} default button 1
	
	repeat with _message in selected_messages
		set message_sender to sender of _message
		set date_received to date received of _message
		set date_string to date_received as string
		set message_content to content of _message
		set message_subject to subject of _message
		
		set note_question to display dialog "Would you like to add a note to the following message?" & return & return & "From: " & message_sender & return & "Subject: " & message_subject & return & "On: " & date_received & return buttons {"Cancel", "No", "Yes"} default button "No"
		set note_question_returned to button returned of note_question
		if note_question_returned is "Yes" then
			set note_entry to display dialog "Please enter a breif note:" default answer ""
			set note_text to text returned of note_entry
			set message_data to {message_sender, message_subject, date_received, note_text, message_content}
		else
			set message_data to {message_sender, message_subject, date_received, message_content}
			set message_data_string to message_data as string
			set the end of message_text to message_data_string
		end if
	end repeat
	
end tell

set character_count to count of message_data_string


if character_count is greater than 3500 then
	set text item delimiters to ""
	set message_characters to every character of message_data_string
	set number_pages to character_count / 3500 as integer
	
	set page_strings_list to {}
	repeat number_pages times
		if (count of message_characters) is less than 3500 then
			set first_3500 to every item of message_characters as string
			set end of page_strings_list to first_3500
			set message_characters to {}
		else
			set first_3500 to items 1 through 3500 of message_characters as string
			set end of page_strings_list to first_3500
			set the_rest to items 3500 through end of message_characters
			set message_characters to the_rest
		end if
	end repeat
	
else
	set page_strings_list to every item of message_characters
end if
--output: page_strings_list -> a list containting each page of the selected message


set message_subject_string to message_subject as string
set message_sender_string to message_sender as string

repeat with _page in page_strings_list
	if note_count is less than 10 then
		set file_number to "00" & note_count
		set new_file_number to file_number + 1
		set new_file_name to new_file_number & message_subject_string & " - " & message_sender
	else
		set file_number to "0" & note_count
		set new_file_number to file_number + 1
		set new_file_name to new_file_number & message_subject_string & " - " & message_sender
	end if
	
	tell application "TextEdit"
		
		
		set Filename to new_file_name --Name the file 
		set file_path to message_folder as string
		set this_data to _page as string
		set target_file to file_path & Filename --You can eliminate "Filename" as a variable and type the filename as part of the path (if you wish) but you need a full path 
		set append_data to false --Set this to false if you want to overwrite, true if you want to add text to an existing file
		
		try --This part writes the file
			set the target_file to the target_file as text
			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
			
			set each_paragraph to every text item of this_data
			
			repeat with _item in each_paragraph
				write _item & return to the open_target_file starting at eof
			end repeat
			
			close access the open_target_file
		on error
			try
				close access file target_file
			end try
			
		end try
	end tell
	
end repeat

Model: PowerBook 1.5Ghz 15"
AppleScript: 1.10
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Also, here’s my script from which the text-writing code was taken. It works perfectly in this context:

set text item delimiters to ", "

tell application "iTunes"
	set this_plist to name of playlist "Newly Added nano Music"
	
	set last_day to every track of playlist this_plist
	set playlist_items to {}
	repeat with _album in last_day
		set album_name to album of _album as string
		set artist_name to artist of _album as string
		
		set artist_album to artist_name & " - " & album_name
		if playlist_items does not contain artist_album & ", " then
			set the end of playlist_items to artist_album & ", "
		end if
		
	end repeat
	
end tell

tell application "TextEdit"
	
	set Filename to "Newly Added Albums" --Name the file 
	
	set this_data to playlist_items as string --Any text here
	
	set target_file to "iPod nano 4GB:Notes:" & Filename --You can eliminate "Filename" as a variable and type the filename as part of the path (if you wish) but you need a full path 
	set append_data to false --Set this to false if you want to overwrite, true if you want to add text to an existing file
	
	try --This part writes the file
		set the target_file to the target_file as text
		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
		
		set each_paragraph to every text item of this_data
		
		repeat with _item in each_paragraph
			write _item & return to the open_target_file starting at eof
		end repeat
		
		close access the open_target_file
	on error
		try
			close access file target_file
		end try
	end try
	--display dialog "Your iPod's Notes have been updated." buttons {"OK"} default button 1
end tell