代码之家  ›  专栏  ›  技术社区  ›  BUKTOP

Applescript:从文件读取文本

  •  2
  • BUKTOP  · 技术社区  · 8 年前

    我无法使用applescript从文本文件中读取文本。尝试了以下操作:

    set sharesFileName to (the POSIX path of (path to home folder)) & "Applications/mounts"
    set sharesLines to paragraphs of (read POSIX file sharesFileName as "class utf8" using delimiter linefeed)
    

    获取“脚本错误”,“文件结束错误”

    我做错了什么?文件确实存在并且可读

    2 回复  |  直到 8 年前
        1
  •  3
  •   user3439894    8 年前

    如果 mounts 是纯文本文件,则 实例 AppleScript 密码 应工作:

    set sharesFileName to (the POSIX path of (path to home folder)) & "Applications/mounts"
    set sharesLines to read sharesFileName
    

    例子:

    $ cat mounts
    one
    two
    three
    $
    

    使用:

    set sharesLines to read sharesFileName
    

    结果:

    "one
    two
    three
    "
    

    使用:

    set sharesLines to read sharesFileName using delimiter linefeed
    

    结果:

    {"one", "two", "three"}
    

    使用:

    set sharesLines to paragraphs of (read sharesFileName)
    

    结果:

    {"one", "two", "three", ""}
    

    如您所见,最后一个示例包括 linefeed 作为文件中的空字符串,其中使用 using delimiter linefeed 不会,并且两者都会创建 list . 所以如果你想 sharesLines 作为 列表 ,然后使用:

    使用分隔符换行符将sharesLines设置为读取sharesFileName
    

    如果你愿意 共享线路 仅包含以下内容 安装件 文本段落 ,然后使用:

    将sharesLines设置为读取sharesFileName
    

    注: 这个 实例 AppleScript 密码 就是这样,不雇佣任何 错误处理 这只是为了显示完成任务的多种方法之一。用户始终有责任添加/使用适当的 错误处理 根据需要/需要。

        2
  •  0
  •   wch1zpink    8 年前

    这适用于我使用最新版本的Sierra(阅读您的评论后,它只是一个简单的文本文件)

    set sharesFileName to (the POSIX path of (path to home folder)) & "Applications/mounts.txt"
    set sharesLines to paragraphs of (read sharesFileName)