Gerald
Gerald

Reputation: 33

Applescript: reading a text file with utf8 class triggers error

I want to read from a text file paragraph by paragraph and since the content of the file is in German language the file contains special characters and I understood I have to use class utf8 in order to read the character properly into the script.

I face the problem if I use the suggested command

set txt to paragraphs of (read foo for (get eof foo)) as «class utf8»

I get the error

error "Can’t make {\"\tDate:\t10. J√§nner 2006 20:53\", \"\tTags:\tHase, Muffin, Paul\", \"\tLocation:\tM√ºhlgasse, Wiener Neudorf, Lower Austria, Austria\", \"\tWeather:\t-7¬∞ Clear\", \......

If I read the file without the «class utf8» no error occurs.

I use the following code:

set theFile to readFile("/Users/Muffin/Documents/DayOne-Export/DayOne.md")
-- set Shows to read theFile using delimiter return
repeat with nextLine in theFile
<text processing>
end repeat

on readFile(unixPath)
    -- prepare text file to read
    set foo to (open for access (POSIX file unixPath))
    set txt to paragraphs of (read foo for (get eof foo)) as «class utf8»
    -- set txt to paragraphs of (read foo) as «class utf8»
    close access foo
    return txt
end readFile

The text file looks like this:

Date:   10. Jänner 2006 20:53<br>
Tags:   Hase, Muffin, Paul<br>
Location:   Mühlgasse, Wiener Neudorf, Lower Austria, Austria<br>
Weather:    -7° Clear<br>

    1st Sign of Paul’s arrival
    .... Actually it was a normal morning and as usual I got up at 6 am start preparing the breakfast.

The error occurs directly in the set txt command.

Any idea why I run into errors?

Upvotes: 3

Views: 3908

Answers (1)

idmean
idmean

Reputation: 14865

Your brackets are placed incorrectly:

set txt to paragraphs of (read foo for (get eof foo) as «class utf8»)

Otherwise you would try to convert a list into utf8.

BTW

for (get eof foo) is unnecessary.

Upvotes: 7

Related Questions