Relative file paths with Applescript

Calion picture Calion · Jun 11, 2014 · Viewed 8.3k times · Source

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?

Answer

dj bazzie wazzie picture dj bazzie wazzie · Jun 11, 2014

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