file - 将文本文件读入 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 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-“权力的游戏”和另外两行这样的。

最佳答案

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

你也不需要整个 开放访问 只是读取文件时的事情,所以你的脚本可以减少到只是

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

关于file - 将文本文件读入 Applescript 中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10064849/

相关文章:

javascript - Jquery/JS 不加载文本文件

java - 如何创建以非 Activity 文本作为后缀的 SWT 文本字段?

linux - 用 shell 脚本比较 2 个文件

c - 如何在 POSIX 中以编程方式写入文件修改日期?

html - 文本叠加隐藏在旁边的图像后面

Applescript 中的 Javascript

applescript - 有没有办法在 AppleScript 中制作自定义库?

browser - 检测 Applescript 中的默认浏览器

python - 在Python中连接来自connection.recv()的数据包

c++ - fwrite 在一台计算机上 float ,在另一台计算机上被 fread