8

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?

Calion
  • 243
  • 2
  • 13

2 Answers2

18

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
dj bazzie wazzie
  • 3,472
  • 18
  • 23
  • 7
    Indeed. Another tip for relative paths, if you want a path relative to the location of the script itself, is to use `(POSIX path of (path to me))`. – Ivan X Jun 11 '14 at 22:03
  • 3
    Yet another tip, `(POSIX path of ((path to me as text) & "::"))` for the folder containing the script from [this answer](http://superuser.com/a/670898) – Franklin Yu Mar 07 '16 at 04:12
0

To accomplish this, you could use a shell script instead of tell application "Finder", or you could use a shell script to get the home folder and insert it into your tell block.

To use a shell script, you can use the following code: do shell script "open ~/Dropbox/Getting\\ Started.pdf. To insert a shell script into your Finder tell block, you could use this code: tell application Finder to open (do shell script "echo $HOME") & "/Dropbox/Getting Started.pdf". This uses a shell script to print the path to the logged in user's home directory and uses it in the path you give Finder.

I hope these suggestions help you solve your problem! ✌️