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

and*_*rsg 5 applescript text file

我尝试创建一个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
Run Code Online (Sandbox Code Playgroud)

当我运行输出时,我得到这个:

error "Can not change \"Game.of.Thrones-\\\"Game Of \" to type file." number -1700 from "Game.of.Thrones-\"Game Of " to file"
Run Code Online (Sandbox Code Playgroud)

我的列表看起来像这样:Game.of.Thrones-"权力的游戏"和另外两行.

小智 6

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

在阅读文件时,您也不需要完全打开访问权限,因此您的脚本可以简化为

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