Relative file paths with Applescript

别来无恙 提交于 2019-12-18 03:42:05

问题


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

works great, whereas

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

fails.

Is there any way to accomplish this simply?


回答1:


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


来源:https://stackoverflow.com/questions/24152552/relative-file-paths-with-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!