【问题标题】:Relative file paths with ApplescriptApplescript 的相对文件路径
【发布时间】:2014-07-31 21:33:43
【问题描述】:

我正在尝试制作一个 Applescript,它会在不知道硬盘驱动器或用户名的情况下打开用户计算机上的文件,假设该文件位于用户目录中的同一位置。

tell application "Finder" to open "/Users/jim/Dropbox/Getting Started.pdf" as POSIX file

效果很好,而

tell application "Finder" to open "~/Dropbox/Getting Started.pdf" as POSIX file

失败。

有没有什么方法可以简单的做到这一点?

【问题讨论】:

    标签: applescript finder


    【解决方案1】:

    您不能在 AppleScript 中使用波浪号路径,因为 POSIX 文件实际上是一个 URL。文件路径的 URL 不支持增量路径,仅支持绝对路径。但是 POSIX 路径中波浪号的含义并没有什么特别之处,它只是被主文件夹所取代。所以要获得相同的结果,我们只需将您的代码更改为:

    tell application "Finder" to open (POSIX path of (path to home folder)) & "Dropbox/Getting Started.pdf" as POSIX file
    

    【讨论】:

    • 确实如此。如果您想要相对于脚本本身位置的路径,则相对路径的另一个提示是使用(POSIX path of (path to me))
    • 另一个提示,(POSIX path of ((path to me as text) & "::")) 用于包含来自this answer 的脚本的文件夹
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2013-10-29
    相关资源
    最近更新 更多