Data Input and Output

Read A Text File

set theFileText to (theFileAsAlias) of me

on ReadFile(theFile) -- (alias) as text
	set theOpenFile to open for access theFile
	set theContent to read theOpenFile
	close access theOpenFile
	return theContent
end ReadFile

Write A Text File

Apple has a great snippet for writing to a file, complete with error handling and toggling appending to a pre-existing file. The following snippet offers a cleaner interface by eliminating the three original arguments and leaving only the text to be written. Add salt to taste because this does simply append to the pre-existing text contained in the file. Generally this isn’t a bad thing, especially if I am writing a log file and I want to the effect of changes across iterations or multiple batches.

my WriteLog("Once upon a time in Silicon Valley...")

on WriteLog(the_text)
	set this_story to the_text -- other formatting can be added here like a time stamp, {return}, etc.
	set this_file to (((path to desktop folder) as text) & "MY STORY")
	my write_to_file(this_story, this_file, true)
end WriteLog