AppleScript:使用utf8类读取文本文件会触发错误

Ger*_*ald 2 applescript utf-8

我想逐段阅读文本文件,并且由于文件的内容是德语,因此文件包含特殊字符,并且我知道我必须使用utf8类才能将字符正确读取到脚本中。

如果使用建议的命令,我会遇到问题

set txt to paragraphs of (read foo for (get eof foo)) as «class utf8»
Run Code Online (Sandbox Code Playgroud)

我得到错误

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\", \......
Run Code Online (Sandbox Code Playgroud)

如果我在没有«class utf8»的情况下读取文件,则不会发生错误。

我使用以下代码:

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
Run Code Online (Sandbox Code Playgroud)

文本文件如下所示:

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.
Run Code Online (Sandbox Code Playgroud)

该错误直接在set txt命令中发生。

知道为什么我会出错吗?

idm*_*ean 5

您的括号放置不正确:

set txt to paragraphs of (read foo for (get eof foo) as «class utf8»)
Run Code Online (Sandbox Code Playgroud)

否则,您将尝试将转换listutf8

BTW

for (get eof foo) 是不必要的。