reading contents of a text file

I know this should be simple; I have the path to a text file (i.e. OSX Drive:Applications:MyApp:config.txt)

I need to read the contents of the text file using applescript; how is this done?

Thanks :slight_smile:


One way would be something like (in a handler):

set myFile to (choose file) as text
set myText to ReadFromFile(myFile)
return myText

on ReadFromFile(theFile)
		open for access file theFile with write permission
	end try
	set theText to read file theFile
		close access file theFile
	on error
		close access file theFile
	end try
	return theText
end ReadFromFile


John M

Assuming that you have the path saved as a string and not a file reference, you can use this…

set filePath to "OSX Drive:Applications:MyApp:config.txt"
set fileContents to read file filePath

Adding ‘try’ blocks is a good idea in case the file isn’t there, but you do not need to open or close the file in order to read it.


Thank you very much. I thought I had tried that, but evidently not :slight_smile: I think my problem might have also been that the filepath was not distinctly cast as a string.

Has it always been true that you don’t need to open a file to read it? Heck, it was only six months ago that I realized there wasn’t a 32K limit anymore! With a 32K limit, you kind of needed to open the file to get the EOF so you could loop through it 32K at a time.