File 将文本文件读入Applescript中的列表

File 将文本文件读入Applescript中的列表,file,text,applescript,File,Text,Applescript,我试图创建一个AppleScript,它读取文本文件并将内容放入列表中。该文件是一个简单的文本文件,其中每一行都是这样的:example-“example” 第一个是文件名,另一个是文件夹名 这是我现在的代码: set listOfShows to {} set theFile to readFile("/Users/anders/Desktop/test.txt") set Shows to read theFile using delimiter return repeat with nex

我试图创建一个AppleScript,它读取文本文件并将内容放入列表中。该文件是一个简单的文本文件,其中每一行都是这样的:
example-“example”

第一个是文件名,另一个是文件夹名

这是我现在的代码:

set listOfShows to {}
set theFile to readFile("/Users/anders/Desktop/test.txt")
set Shows to read theFile using delimiter return
repeat with nextLine in Shows
    if length of nextLine is greater than 0 then
        copy nextLine to the end of listOfShows
    end if
end repeat
choose from list listOfShows


on readFile(unixPath)
    set foo to (open for access (POSIX file unixPath))
    set txt to (read foo for (get eof foo))
    close access foo
    return txt
end readFile
当我运行该输出时,我得到以下结果:

error "Can not change \"Game.of.Thrones-\\\"Game Of \" to type file." number -1700 from "Game.of.Thrones-\"Game Of " to file"

我的列表如下所示:Game.of.Thrones-“Game of Thrones”和另外两行类似的内容。

错误在于您试图将文件(您读取的第一个文件)的内容作为文件读取。获取文本的段落将在返回/换行边界处将其分开,这通常比猜测文件中使用了什么行尾字符效果更好

在读取文件时,您也不需要整个打开以访问内容,因此您的脚本可以简化为

set listOfShows to {}
set Shows to paragraphs of (read POSIX file "/Users/anders/Desktop/test.txt")
repeat with nextLine in Shows
    if length of nextLine is greater than 0 then
        copy nextLine to the end of listOfShows
    end if
end repeat
choose from list listOfShows

read
默认情况下使用MacRoman,因此它会在UTF-8文件中混淆非ASCII字符,除非您将
添加为«类utf8»
。(
作为Unicode文本
是UTF-16。)

的段落也适用于CRLF和CR行结尾。这不会,但如果最后一行为空,则会忽略它:

read POSIX file "/tmp/test.txt" as «class utf8» using delimiter linefeed

AppleScript的语言参考说明见第120页:

一系列字符,从上一段结尾后的第一个字符或文本开头开始,以回车符(\r)、换行符(\n)、换行符/换行符对(\r\n)或文本结尾结尾。不支持Unicode“段落分隔符”字符(U+2029)。

因此,U+8232被忽略,AppleScript返回文件中的全部文本


U+8232在文本编辑中用作CR字符…

它在我的输出中仍然只读取“Game.of.Thrones-\“Game of”,我现在没有收到任何错误。我尝试了文本文件中的文本:1文本,2文本…5文本。结果是1文本,2文本,3文本,4文本,5。不是5文本脚本只获取文件的每一行(由换行符(如回车符)分隔)并将其放入一个列表中-这不是你问的吗?不确定为什么文件不能完全读取,除非你没有使用纯读命令,并且正在处理分隔符或读取的字符数。我只想读取文件中的每一行。但由于某些原因,它无法输出所有内容?我会尝试更多,明白吗以正确的方式,谢谢:)问题是,它只需要文本文件中的前23个字符。我不知道是什么原因造成的?虽然此代码可能会回答此问题,但提供有关为什么和/或如何回答此问题的附加上下文将显著提高其长期价值。请您的回答添加一些解释。ASLR提到U+2029不受支持,但它忽略了提及applescript也不支持的U+2028。它们分别是段落分隔符和行分隔符。如文本所示,它们是Unicode字符,除了8232和8233实体之外,浏览器似乎不太支持。请参阅。
set milefile to ((path to desktop as text) & "Alert.txt")
set theFileContents to (read file milefile)
display dialog theFileContents
set milefile to ((path to desktop as text) & "Alert.txt")
set theFileContents to (read file milefile)
display dialog theFileContents