使用Applescript的相对文件路径

Cal*_*ion 6 applescript finder

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

tell application "Finder" to open "/Users/jim/Dropbox/Getting Started.pdf" as POSIX file
Run Code Online (Sandbox Code Playgroud)

效果很好,而

tell application "Finder" to open "~/Dropbox/Getting Started.pdf" as POSIX file
Run Code Online (Sandbox Code Playgroud)

失败.

有没有办法简单地完成这个?

dj *_*zie 12

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

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

  • 确实.相对路径的另一个提示,如果你想要一个相对于脚本本身位置的路径,就是使用`(POSIX路径of(path to me))`. (5认同)
  • 还有一个提示,`(POSIX路径为((作为文本的路径)和"::"))`用于包含来自[this answer]的脚本的文件夹(http://superuser.com/a/670898) (2认同)