opening text file and conversion to string.

I have an easy problem but it is very confusing. I am trying to open a file that is a textedit file and convert it into a string.
I got the textedit file from the web from the automator command “get text from website”. There are so many ways to open the file but no clear explanation on how to get it to a string. I have tried the read command to ill effect. It seems most of my commands keep copying my path “///text.txt” as text.
I did get one to work however but it returns a bunch of characters that look like crosses in the text.
This is the script I used.
tell application “Finder”
set textfile to (read (“/Users///text.txt” as POSIX file))
return textfile
end tell

Model: mac mini core duo
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Try running something like this in Script Editor:

set f to choose file

You’ll see how to reference files with an alias reference. To read the file:

set f to choose file
set t to read f

This read read text files. If the file is rich text, then you need to read it some other way.



Are you really saving the file to your “Users” folder? :confused: If so, and it’s a plain text file with that name, then your script should work “ though you don’t need the Finder ‘tell’ block.

With the Automator on my machine, there’s a Safari action called “Get Text from Webpage” (not “website”). As far as I can see, it doesn’t save the text it returns to anywhere you can specify, so there must be another action in the workflow. I tried adding the TextEdit action “New Text File” to save the text to file. The text in the resulting file appears to be Unicode, in which case you’d need to read it ‘as unicode text’ and then coerce the result to string (if that’s what you really want to do).

set unicodeTxt to (read ("/Users///text.txt" as POSIX file) as unicode text) 
set {text:stringTxt} to unicodeTxt as string
return stringTxt

But it would probably be less fuss to leave out Automator and to script Safari to fetch the page and return the text directly to your script.

Thanks both to posting.
The characters were present when I didn’t declare unitext. Unitext removed them. I am not sure why I didn’t see the text before but there were a lot of spaces in the beginning and I didn’t realize there was text out of sight in the result box.
It works like a charm!