Applescript 的相对文件路径 [英] Relative file paths with Applescript

查看:35
本文介绍了Applescript 的相对文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make an Applescript that will open a file on a user's computer without knowing the hard drive or user name, presuming the file is in the same place in the user directory.

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

失败.

有什么办法可以简单地做到这一点吗?

Is there any way to accomplish this simply?

推荐答案

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

You can't use tilde paths in AppleScript basically because POSIX file is in fact an URL. URLs for file paths doesn't support incremental paths, only absolute paths. But the meaning of the tilde in POSIX paths is not something special, it's just replaced by the home folder. SO to get the same results we only need to change your code to:

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

这篇关于Applescript 的相对文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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