0

我已经搜索了几个小时,每次我尝试推荐的修复。我仍然无法到达任何地方。我觉得我错过了一些如此明显的东西,我的 Mac 正在嘲笑我糟糕的尝试。

这是脚本:

tell application "System Events"
set the_folder to path to folder "dropbox" from user domain as string
set the_file to "ToDo.txt" of (POSIX path of the_folder)
set the_text to (do shell script "cat " & quoted form of (POSIX path of the_file))
return the_text
end tell

结果:

无法获取(the_folder 的 POSIX 路径)的“ToDo.txt”。不允许访问。

文件夹是什么也没关系。我已经尝试过使用 Documents/Library,但仍然总是遇到访问问题。

4

1 回答 1

1

这是您的脚本的更正版本:

    tell application "System Events"
        set the_folder to the folder "~/Dropbox"
        set the_file to the file "ToDo.txt" in the_folder

        set the_text to do shell script "cat " & ¬
            quoted form of (POSIX path of the_file as text)
    end tell

    return the_text

需要注意的几点如下:

  1. 不要Path To用于未被内置 AppleScript 常量引用的文件夹,例如home folderor desktop folder。相反,我将这一行更改为对folder具有指定路径的对象的简单引用"~/Dropbox"
  2. 同样,您需要file在说明文件名之前放置一个对象说明符,否则您所做的只是给系统事件一段文本,并说该文本在文件夹中的某个位置(这不会产生很多感觉)。现在我告诉系统事件它是 afile并且文本是文件名,它知道在哪里可以找到它。
  3. 最后,出于某种原因,您需要声明它POSIX path of the_file的类型是 class text。我真的不知道为什么 AppleScript 看不到它已经是文本,但有时就是这样。

现在我将向您展示另一个脚本,它将完全按照您的方式执行:

    set the_text to read (POSIX path of ¬
        (path to home folder) & ¬
        "Dropbox/ToDo.txt" as POSIX file as alias)
于 2018-06-26T05:14:11.037 回答