preview a text file

can anyone suggest a way to preview a text file without opening it
I found this somewhere but it gets a finder error:

tell application "Finder"
	activate
	if selection is equal to {} then
		choose file with prompt "Choose a text file to preview"
		set theFile to the result
	else
		set theFile to selection
	end if
	open for access file theFile
	get eof of theFile
	set fileLength to the result
	if fileLength is greater than 255 then
		read theFile for 255
	else
		read theFile
	end if
	set theData to the result
	display dialog theData
	close access theFile
end tell

think it might be one of Emmanuels, not sure
thanks,
Peter

This worked for me:


tell application "Finder"
	activate
	if selection is equal to {} then
		choose file with prompt "Choose a text file to preview"
		set theFile to the result
	else
		set theFile to selection
	end if
	open for access (theFile as alias)
	get eof of theFile
	set fileLength to the result
	if fileLength is greater than 255 then
		read theFile for 255
	else
		read theFile
	end if
	set theData to the result
	display dialog theData
	close access theFile
end tell

Hope that helps.
i changed open for access file theFile to what is written above.

I had the same problem doing something else.

thanks
I still get the error:
Finder got an error: Can’t make {document file “Contacts Export.txt” of folder “WFA” of folder “Documents” of folder “p” of folder “Users” of startup disk} into a file.

I am using OSX
how about you?

cheers
P

osX too. Hmmmm, i will try and see what i can do.

Sorry but my knowledge isnt much, only been doing it for 7 weeks. I’l lhave a look though.

Sounds like the referncing of the file is doing it.

Take out the as alias from the file and see what happens

Bravo!
this works:



tell application "Finder"
	activate
	if selection is equal to {} then
		choose file with prompt "Choose a text file to preview"
		set theFile to the result
	else
		set theFile to selection
	end if
	open for access theFile
	get eof of theFile
	set fileLength to the result
	if fileLength is greater than 255 then
		read theFile for 255
	else
		read theFile
	end if
	set theData to the result
	display dialog theData
	close access theFile
end tell

thanks for the help
Peter :slight_smile:

What happens when more than one file is selected?

– Rob

errrrrrrrrrrrrr… not a lot :slight_smile:

it breaks