在AppleScript中将文本文件读入列表 [英] Read textfile into list in Applescript

查看:28
本文介绍了在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

这篇关于在AppleScript中将文本文件读入列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆