Applescript:用utf8类读取文本文件触发错误

时间:2014-06-14 09:45:36

标签: utf-8 applescript

我想逐段读取文本文件,因为文件的内容是德语,文件包含特殊字符,我知道我必须使用类utf8才能正确地将字符读入脚本。

如果我使用建议的命令

,我将面临问题
set txt to paragraphs of (read foo for (get eof foo)) as «class utf8»

我收到错误

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\", \......

如果我在没有«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

文本文件如下所示:

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.

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

知道我为什么会遇到错误吗?

1 个答案:

答案 0 :(得分:4)

您的括号放错了。正确它必须如下所示:

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

否则,您会尝试将list转换为utf8

顺便说一句

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