2014-06-14 65 views
1

我想從段落中讀取文本文件,因爲該文件的內容是德語,該文件包含特殊字符,我明白我必須使用類utf8以便將字符正確地讀入腳本中。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\", \...... 

如果我讀的文件,而不«類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命令直接出現的錯誤。

任何想法爲什麼我遇到錯誤?

回答

3

您的括號錯誤。更正必須是這樣的:

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

否則,你會嘗試轉換成listutf8

BTW

​​是不必要的。